问题
I'm creating my first android app (or attempting to anyway) and i have a question i can't seem to find the answer to.
I would like to allow users to enter a separate set of preferences for each item in a list view. I have the PreferenceScreen working, but it maintains the same preferences for every item (i.e. changing 1 item results in changes to all others as well).
To give a more detailed example: Say i have a list of cars: * Car 1 * Car 2 * Etc...
I would like to be able to click on "Car 1" and get a PreferencesScreen to specify "Make", "Model", "Color" for just that car, instead of having those entries stored globally for the whole app.
Is this possible?
Thanks for your help!
//N
回答1:
Solved it!!
For any wondering, the solution is that you must call
getPreferenceManager().setSharedPreferenceName(value)
with a value unique to the item you'd like to modify the preferences for (in my case, i used the ListView
position) prior to calling addPreferencesFromResource
.
回答2:
getPreferenceManager()
deprecated on api 11
Now with honeycom and ICS the method getPreferenceManager()
is deprecated. Do you know how to do that with the new (PreferenceFragment + Headers)?
Thanks men! It works perfect in earlier versions!
UPDATE: Up to api level 11 is the same method but you have to call it on a Fragment, not on activity.
来源:https://stackoverflow.com/questions/3217720/individual-preferences-for-each-item-in-a-listview