开发者

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:

  1. User goes into the app's preferences
  2. User selects the button to change the default item
  3. My activity launches, and the user selects an item from that list
  4. My activity finishes, having set the result
  5. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜