Is it possible to use resource IDs instead of their values for xml preferences in Android?

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

The Goal

In my preferences.xml I would like to store the ID of certain string elements (like) e.g. ...

<string-array name="preference_values">     <item>@string/nav_start</item>     <item>@string/nav_overview</item>     <item>@string/nav_someelse</item> </string-array> 

... meanwhile in xml/pref_general.xml ...

<ListPreference     android:key="@string/preference_key"     android:entryValues="@array/preference_values"     ... /> 

... where the different strings will be localized:

Since I don't want to store the localized strings (as those xml definitions do right now), but rather their integer keys, I would really like to know if there is a way of doing this.

This would make the preferences still available after a language change for example.

More Background

I really like being able to access/compare resources in code, which are not subjective to typos or name changes (or at least the IDE checks/handles those for you) e.g.:

switch( getMyPreferenceAsInt(R.string.preferenece_key) ) {      case R.string.nav_start:    doSome();     break;     case R.string.nav_someelse: doSomeElse(); break;     ... } 

I use something similar for my NavigationDrawer, and find it really nice to work with. This is also where does string values actually come from (it's like a "default start page" preference).

<string-array name="navigation_keys">     <item>@string/nav_start</item>     <item>@string/nav_overview</item>     <item>@string/nav_someelse</item>     ... </string-array> 

But at least here I am able to get the keys with obtainTypedArray(...):

void onCreate(Bundle savedInstanceState) {     ...     mNavKeys = getResources().obtainTypedArray(R.array.navigation_keys); }  void onNavigationDrawerItemSelected(int position) {      Fragment fragment;     int selectedResource = mNavKeys.getResourceId(position, R.string.nav_default);      if (selectedResource == R.string.nav_start) {         fragment = FancyStartFragment.newinstance();     }     else {         fragment = ...;     }      // Fancy methods to do a fragment transaction     ... } 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!