java - Dynamically adding options to a listpreference -
i writing program has preferenceactivity
, multiple preferencefragment
. 1 of options user selects server connect to, populated xml.
<string-array name="server_names"> <item>test</item> <item>item1</item> <item>item2</item> </string-array> <string-array name="server_addresses"> <item>10.10.10.1</item> <item>10.10.10.2</item> <item>10.10.10.3</item> </string-array>
this works fine , 3 names in list. there separate fragment allows user enter name , ip address added drop down option.
i have got working solution involved loading external file, clearing entries , adding ones file. 'ok' want use sharedpreferences
save these values. question this, how use editor write options saved each time app launched?
i have looked @ using editor
, putstringset
, commit
added option doesn't appear in drop down. there related posts seem deal textpreference
solutions haven't solved problem.
edit, how create listpreference:
<listpreference android:entries="@array/server_names" android:entryvalues="@array/server_addresses" android:key="@string/countrylistid" android:negativebuttontext="@null" android:positivebuttontext="@null" android:title="@string/pref_title_select_com_target" android:enabled="true" android:shoulddisableview="false" />
i have click handler on label add listpreference:
public boolean onpreferenceclick(preference preference) { new alertdialog.builder(getactivity()) .settitle("add new server") .setmessage("confirm wish add server?") .seticon(android.r.drawable.ic_dialog_alert) .setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { sharedpreferences sharedpref = getactivity().getpreferences(context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); /* command in here edit server_names , server_addresses */ }}) .setnegativebutton(android.r.string.no, null).show(); return true; } });
write shared pref
sharedpreferences sharedpref = getactivity().getpreferences(context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); editor.putint("highscore", 5); editor.commit();
read from:
sharedpreferences sharedpref = getactivity().getpreferences(context.mode_private); int defaultvalue = 0; long highscore = sharedpref.getint("highscore"), defaultvalue);
is asking?
edit:
looking @ code, seem missing actual saving.
check answer gregor: how add new value listpreference , save it?
he says everytime open dialog, list xml loaded. need change behaviour include preferences sharedpreferences.
you can change behavior using setentries() , setentryvaues() methods of listpreference
Comments
Post a Comment