java - Strange values in onPreferenceChange method -


i have android app preferences screen. preference in screen multiselectlistpreference 4 options. i'm trying implement onpreferencechange method update preference's summary every time option changes.

to this, i'm casting newvalue object hashset of strings, when log out contents of hashset, don't correspond values user selected.

this code:

@override public boolean onpreferencechange(preference preference, object newvalue) {     if (preference instanceof multiselectlistpreference) {         multiselectlistpreference pref = (multiselectlistpreference) preference;         hashset<string> values = (hashset<string>) newvalue;         log.e("settingsactivity", values.tostring());     }      return true; } 

and here log message printed out when select 4 options:

07-28 13:13:00.896 31196-31196/com.example.android.theguardiannews e/settingsactivity: [ film , environment,  business ,  environment , politics, business] 

edit: here image of possible options:

know why happening?

so can try inside onpreferencechange() method:

sharedpreferences sharedprefs = preferencemanager.getdefaultsharedpreferences(this); set<string> selections = sharedprefs.getstringset("your_preference_key", null); string[] selected = selections.toarray(new string[] {}); toast.maketext(context, selected[all], toast.length_long).show(); 

and update sumary add line:

preference pref = findpreference("pref_key"); pref.setsummary(selected[all]); 

so code like:

public boolean onpreferencechange(preference preference, object newvalue) {     sharedpreferences sharedprefs =   preferencemanager.getdefaultsharedpreferences(this);     set<string> selections = sharedprefs.getstringset("your_preference_key", null);     string[] selected = selections.toarray(new string[] {});     preference pref = findpreference("your_pref_key");     pref.setsummary(selected[all]);      return true; } 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -