Using a PreferenceActivity to let the user pick something from a lsit
I have created an activity which when launched, displays a list of items, allowing the user to create, rename, delete, etc. items from this list. Once the user clicks an item on the list, the activity calls setResult()
followed by finish()
. I use this activity various times in my application, and want to use it to let the user select 开发者_开发技巧a default value from this list from a PreferenceActivity. So basically, I want it to flow like this:
- User goes into the app's preferences
- User selects the button to change the default item
- My activity launches, and the user selects an item from that list
- My activity finishes, having set the result
- The value the user selected is now saved as the default value as a preference in the application
Anyone know how to do this?
You need to set an onPreferenceClickListener()
similar to the following:
xml:
<Preference
android:title="My Preferences"
android:summary="A Preferences I am going to set"
android:key="myPref"/>
code:
Preference myPref= (Preference) findPreference("myPref");
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
//LAUNCH YOUR ACTIVITY
return true;
}
});
Then receive the returned value in onActivityResult()
and saved it into the preferences.
@segfault Intent i = new Intent(NameOfClass.class, MyActivity.class); this will work sure.
精彩评论