以下是我定制的public class Preferences
中的一些代码,它们可以在SharedPreferences中正确存储和检索值:
public static final String DIR_SP_FILTERS = "filters";
public static final String KEY_SP_FILTERS_LIST = "flist";
public static final String DEFAULT_FILTERS = "1111110";
private static SharedPreferences f_sP;
public static void setUpPrefs(Context context, MapDisplay myMap) {
// Load filter preferences
f_sP = context.getSharedPreferences(DIR_SP_FILTERS, Context.MODE_PRIVATE);
String masterFilters = f_sP.getString(KEY_SP_FILTERS_LIST, "");
Menu menu = MainActivity.menu;
if(masterFilters.length() != 0) {
for(int i = 0; i < masterFilters.length(); i++) {
int value = Integer.parseInt(masterFilters.charAt(i) + "");
// Dans l'ordre du menu!
switch(i) {
case 0: // Feu
MapDisplay.feuFilter = filterTypeBtn(value, menu.findItem(R.id.cB_fire));
case 1: // Eau
MapDisplay.eauFilter = filterTypeBtn(value, menu.findItem(R.id.cB_water));
case 2: // Terrain
MapDisplay.terrainFilter = filterTypeBtn(value, menu.findItem(R.id.cB_terrain));
case 3: // Météo
MapDisplay.meteoFilter = filterTypeBtn(value, menu.findItem(R.id.cB_meteo));
case 4: // MZ
MapDisplay.showMonitoredZones = filterTypeBtn(value, menu.findItem(R.id.cB_zones));
case 5: // UserPins
MapDisplay.showUserPins = filterTypeBtn(value, menu.findItem(R.id.cB_users));
case 6: // Histo
MapDisplay.historiqueFilter = filterTypeBtn(value, menu.findItem(R.id.cB_histo));
}
}
} else {
// If first time launching app: initialize default filter string
SharedPreferences.Editor editor = f_sP.edit();
editor.putString(KEY_SP_FILTERS_LIST, DEFAULT_FILTERS);
editor.apply();
}
}
对于您来说,这段代码中最重要的部分,我相信是开头声明的常量,它们代表我要保存信息的文件夹和子文件夹的名称。
设置SharedPreferences
工作正常,我确保在关闭应用程序后可以检索指定的值,并且正确保存了来自用户输入的修改(我不会显示保留这些方法事情很简单)。
然后我继续让AndroidStudio为我自动生成一个SettingsActivity,并希望为用户增加至少看到他保存在内存中的此String的内容的可能性(“ 1111110”)。
因此,我将此添加到了SettingsActivity.java
:
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class FiltersPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_monitored_zones);
setHasOptionsMenu(true);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference(Preferences.DIR_SP_FILTERS));
bindPreferenceSummaryToValue(findPreference(Preferences.KEY_SP_FILTERS_LIST)););
}
// ...
}
并确保将我的片段添加到此列表中:
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| GeneralPreferenceFragment.class.getName().equals(fragmentName)
|| DataSyncPreferenceFragment.class.getName().equals(fragmentName)
|| NotificationPreferenceFragment.class.getName().equals(fragmentName)
|| FiltersPreferenceFragment.class.getName().equals(fragmentName); // here
}
请注意我认为是罪魁祸首的两条重要路线:
bindPreferenceSummaryToValue(findPreference(Preferences.DIR_SP_FILTERS));
bindPreferenceSummaryToValue(findPreference(Preferences.KEY_SP_FILTERS_LIST)););
他们使用的是我生成活动时已经定义的这种方法:
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
我确实将片段添加到pref_headers.xml
文件中,并创建了pref_filters.xml
(主要是从另一个pref_*.xml
复制粘贴,因为我仍然不确定如何处理):< / p>
<SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="@string/pref_description_social_recommendations"
android:title="@string/pref_title_social_recommendations" />
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
android:capitalize="words"
android:defaultValue="@string/pref_default_display_name"
android:inputType="textCapWords"
android:key="filters"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_display_name" />
<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
android:defaultValue="-1"
android:entries="@array/pref_example_list_titles"
android:entryValues="@array/pref_example_list_values"
android:key="example_list"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_add_friends_to_messages" />
我的理解是,使这一切正常工作的关键是正确使用android:key="filters"
,方法是使其指向正确的文件夹或子文件夹,如我先前显示的两行中所列出的({{1 }}),但我不确定在那里使用什么值来获得所需的东西。
感谢您的帮助! :)