What's Your Preference?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helper=new RestaurantHelper(this);
prefs=PreferenceManager.getDefaultSharedPreferences(this);
name=(EditText)findViewById(R.id.name);
address=(EditText)findViewById(R.id.addr);
notes=(EditText)findViewById(R.id.notes);
types=(RadioGroup)findViewById(R.id.types);
initList();
prefs.registerOnSharedPreferenceChangeListener(prefListener);
}
Also, we can call initList() from prefListener:
private SharedPreferences.OnSharedPreferenceChangeListener prefListener=
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences sharedPrefs,
String key) {
if (key.equals("sort_order")) {
initList();
}
}
};
At this point, if you recompile and reinstall the application, you should see
the sort order change immediately as you change the order via the
preferences.
Extra Credit................................................................................................
Here are some things you can try beyond those step-by-step instructions:
- Add a preference for the default type of restaurant (e.g., take-out).
Use that preference in detail forms when creating a new restaurant. - Add an option menu to the detail form activity and have it be able
to start the preference activity the way we did from the option
menu for the list. - Rather than use preferences, store the preference values in a JSON
file that you read in at startup and re-read in onResume() (to find out