Back to project page Weather.
The source code is released under:
Apache License
If you think the Android project Weather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.volitic.weather; /*www . j ava 2 s .c om*/ import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.preference.SwitchPreference; import android.widget.CursorAdapter; public class SettingsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.settings_container, new SettingsFragment()) .commit(); } } /** * A preferences fragment for the user to configure settings. */ public static class SettingsFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences); // Bind the summaries to be updated on value change. bindPreferenceSummaryToValue(findPreference("display_units")); bindPreferenceSummaryToValue(findPreference("set_location_manual")); bindPreferenceSummaryToValue(findPreference("allow_mobile_data")); bindPreferenceSummaryToValue(findPreference("location_manual")); bindPreferenceSummaryToValue(findPreference("timezone_utc_offset")); } /** * When a preference is changed, update the summary text of that preference to reflect the * new value. */ private static void bindPreferenceSummaryToValue(Preference preference) { // make sure the preference exists if (preference == null){ return; } // register the change listener preference.setOnPreferenceChangeListener(preferenceValueChangeListener); // Trigger the listener immediately to initialize the summaries // to their current value. if( preference instanceof SwitchPreference ){ preferenceValueChangeListener.onPreferenceChange(preference, PreferenceManager .getDefaultSharedPreferences(preference.getContext()) .getBoolean(preference.getKey(), false)); } else { preferenceValueChangeListener.onPreferenceChange(preference, PreferenceManager .getDefaultSharedPreferences(preference.getContext()) .getString(preference.getKey(), "")); } } /** * A preference value change listener that updates the preference's summary * to reflect its new value. */ private static Preference.OnPreferenceChangeListener preferenceValueChangeListener = new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object value) { String stringValue = value.toString(); String key = preference.getKey(); if( key != null && key.equals("allow_mobile_data")){ //special case if((Boolean)value){ preference.setSummary(R.string.mobile_data_allowed); } else { preference.setSummary(R.string.mobile_data_disallowed); } } else if (preference instanceof ListPreference) { // For list preferences, look up the correct display value in // the preference's 'entries' list. ListPreference listPreference = (ListPreference) preference; int index = listPreference.findIndexOfValue(stringValue); // Set the summary to reflect the new value. preference.setSummary( index >= 0 ? listPreference.getEntries()[index] : null); } else { // For all other preferences, set the summary to the value's // simple string representation. preference.setSummary(stringValue); } return true; } }; } }