import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
+import uk.me.njae.sunshine.data.WeatherContract;
+
/**
* A {@link PreferenceActivity} that presents a set of application settings.
* <p>
public class SettingsActivity extends PreferenceActivity
implements Preference.OnPreferenceChangeListener {
+ // since we use the preference change initially to populate the summary
+ // field, we'll ignore that change at start of the activity
+ boolean mBindingPreference;
+
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
// updated when the preference changes.
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
+ bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key)));
+
}
/**
* is changed.)
*/
private void bindPreferenceSummaryToValue(Preference preference) {
+ mBindingPreference = true;
+
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(this);
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
+
+ mBindingPreference = false;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
- String stringValue = value.toString();
+ String stringValue = value.toString().trim();
+
+ // are we starting the preference activity?
+ if ( !mBindingPreference ) {
+ if (preference.getKey().equals(getString(R.string.pref_location_key))) {
+ FetchWeatherTask weatherTask = new FetchWeatherTask(this);
+ String location = value.toString();
+ weatherTask.execute(location);
+ } else {
+ // notify code that weather may be impacted
+ getContentResolver().notifyChange(WeatherContract.WeatherEntry.CONTENT_URI, null);
+ }
+ }
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in