Android MultiSelectListPreference with priority order -
Android MultiSelectListPreference with priority order -
i'm new android. want utilize multiselectlistpreference case.
but encounter problem: list need maintain order of element. assume there're 5 elements:
0 - tom 1 - david 2 - bob 3 - mary 4 - chris
and user take 0, 2, 3. list must in order below:
tom, bob, mary
but multiselectlistpreference stores setting in set<string>
, not arraylist<string>
, it's not sure order because of set
.
how can create sure order? give thanks you.
camdaochemgio, understood question before edit.
since talking set (that stores unique values) , getvalues() function needs fed own revertvalues function translates values indexes - based on preset of data. asked code express myself writing solution in own style/terminology.
the solution:i noticed in docs of multiselectlistpreference next method :
int findindexofvalue(string value)
but not store such reference object, created class extend multiselectlistpreference (in new file!) :
public class dataholder extends multiselectlistpreference { // note: attributeset needed in super class public dataholder(context context,attributeset attrs) { super(context, attrs); list<charsequence> entries = new arraylist<charsequence>(); list<charsequence> entriesvalues = new arraylist<charsequence>(); /** utilize string array did in q, * preffer way of populating info - * keeps things open , unlimitted. * if want info picked xml , utilize : * context.getresources().getstringarray(r.array.entries) , * context.getresources().getstringarray(r.array.entryvalues) * */ entries.add("0"); entries.add("1"); entries.add("2"); entries.add("3"); entries.add("4"); entriesvalues.add("tom"); entriesvalues.add("david"); entriesvalues.add("bob"); entriesvalues.add("mary"); entriesvalues.add("chris"); setentries(entries.toarray(new charsequence[5])); setentryvalues(entriesvalues.toarray(new charsequence[5])); } }
now need plug in listener. in settingsfragment class , add together new field :
private dataholder dh = null;
and alter constructor take , initialize :
public settingsfragment(context c) { dh = new dataholder(c,null); }
next step: remove reference info xml. should :
<preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" > <com.example.multiselectpref.dataholder android:key="pref_key_name_choice" android:title="@string/name_choice" /> </preferencescreen>
back listener, in onsharedpreferencechanged method , can alter toast :
toast_message += (dh.findindexofvalue(name) + ": "+name+" , ");
works me.. (code committed fork @ https://github.com/li3ro/multiselectpref)
android android-preferences android-settings multiselectlistpreference
Comments
Post a Comment