variables - Android get sharedPreferences to store user choices -



variables - Android get sharedPreferences to store user choices -

i want display the fruit of selection drawable have saves don't know how activity preference , utilize it

i thinking there might somthing fruit = getpreference (something this)

int resourceid = getresource.getidentifier(fruit,"drawable",getpackagename()); favortiefruit.setimageresource(resourceid);

options.java

import android.app.activity; import android.content.sharedpreferences; import android.os.bundle; import android.preference.preference; import android.preference.preference.onpreferenceclicklistener; import android.preference.preferenceactivity; public class options extends preferenceactivity{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.options); //get custom preference preference custompref = (preference) findpreference("listpref"); custompref.setonpreferenceclicklistener(new onpreferenceclicklistener() { public boolean onpreferenceclick(preference preference) { sharedpreferences customsharedpreference = getsharedpreferences("fruits", activity.mode_private); sharedpreferences.editor editor = customsharedpreference.edit(); editor.commit(); homecoming true; } }); } }

options.xml

<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preferencecategory android:title="fruit selection" android:key="listpref"> <listpreference android:title="fruit #1" android:key="listpref1" android:defaultvalue="digigreen" android:entries="@array/fruits" android:entryvalues="@array/listvalues" android:summary="select fruit #1 "/><listpreference android:title="fruit #2" android:summary="select fruit #2 " android:key="listpref2" android:defaultvalue="digigreen" android:entries="@array/fruits" android:entryvalues="@array/listvalues" /><listpreference android:title="fruit #3" android:key="listpref3" android:defaultvalue="digigreen" android:entries="@array/fruits" android:entryvalues="@array/listvalues" android:summary="select fruit #3 "/><listpreference android:title="fruit #4" android:key="listpref4" android:defaultvalue="digigreen" android:entries="@array/fruits" android:entryvalues="@array/listvalues" android:summary="select fruit #4 "/><listpreference android:title="fruit #5" android:key="listpref5" android:defaultvalue="digigreen" android:entries="@array/fruits" android:entryvalues="@array/listvalues" android:summary="select fruit #5 "/><listpreference android:title="fruit #6" android:key="listpref6" android:defaultvalue="digigreen" android:entries="@array/fruits" android:entryvalues="@array/listvalues" android:summary="select fruit #6 "/> </preferencecategory> </preferencescreen>

strings.xml

<string-array name="fruits"> <item>apple</item> <item>pear</item> <item>grape</item> <item >berry</item> <item >tomato</item> </string-array> <string-array name="listvalues"> <item>apple</item> <item>pear</item> <item>grape</item> <item>berry</item> <item>tomato</item> </string-array>

============================== found solution

options.java

import android.os.bundle; import android.preference.preferenceactivity; public class options extends preferenceactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.preferences); } }

getting settings

sharedpreferences sp = preferencemanager .getdefaultsharedpreferences(this); (int num = 1; num < 7; num++) { string icon = sp.getstring("listpref" + num, null); seek { class<drawable> res = r.drawable.class; field field = res.getfield(icon); politiciansarray[num] = field.getint(null); class<string> stringres = r.string.class; field stringfield = stringres.getfield(icon); sayingsarray[num] = stringfield.getint(null); } grab (exception e) { log.e("mytag", "failed", e); }

you should add together preferencechangelistener listpreference

this illustration of how newly selected value listpreference:

final listpreference mypreference = (listpreference) findpreference("listpref"); mypreference.setonpreferencechangelistener(new onpreferencechangelistener() { // @override public boolean onpreferencechange(preference preference, object newvalue) { sharedpreferences customsharedpreference = getsharedpreferences("fruits", activity.mode_private); sharedpreferences.editor editor = customsharedpreference.edit(); editor.putstring("favfruit", (string) newvalue); editor.commit(); homecoming true; } });

when want value saved on sharedpreferences, this:

sharedpreferences settings = getsharedpreferences("fruits", activity.mode_private); string fruit= settings.getstring("favfruit",null);

then, drawable:

int resourceid = getresource.getidentifier(fruit,"drawable",getpackagename()); favortiefruit.setimageresource(resourceid);

android variables drawable sharedpreferences

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -