http post - Store more than one value in Android app preferences using the same key -
i'm not sure on how can store more 1 value in shared preferences on android app using same key.
what app show list , button add item favorites want store numeric value preferences , when user go favorite list send stored favorites through http post in array.
edit: i'm doing way when add new value overrides last one, check implode method, add new value stored list empty or not has add new value , preserve last ones.
import java.util.arraylist; import java.util.list; import java.util.stringtokenizer; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import android.content.context; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.preference.preferencemanager; public class favoriteactivity { private static string favorite_list = "fav_list"; private static sharedpreferences sharedpreference; private static editor sharedprefeditor; public static list<namevaluepair> getfavoritelist(context context) { if (sharedpreference == null) { sharedpreference = preferencemanager .getdefaultsharedpreferences(context); } string storedlist = sharedpreference.getstring(favorite_list, ""); return explode(storedlist); } public static void savefavorite(string fav, context context) { if (sharedpreference == null) { sharedpreference = preferencemanager .getdefaultsharedpreferences(context); } sharedprefeditor = sharedpreference.edit(); implode(getfavoritelist(context), fav, 1); sharedprefeditor.putstring(favorite_list, fav); sharedprefeditor.commit(); } public static list<namevaluepair> explode(string string) { stringtokenizer st = new stringtokenizer(string, ","); list<namevaluepair> v = new arraylist<namevaluepair>(); (; st.hasmoretokens();) { v.add(new basicnamevaluepair("id[]", st.nexttoken())); } return v; } public static string implode(list<namevaluepair> list, string value, int mode) { stringbuffer out = new stringbuffer(); switch (mode) { case 0: list.remove(new basicnamevaluepair("id[]", value)); break; case 1: list.add(new basicnamevaluepair("id[]", value)); break; } boolean first = true; (namevaluepair v : list) { if (first) first = false; else out.append(","); out.append(v.getvalue()); } return out.tostring(); } }
you can't this. if use same key anywhere, overwrite previous value.
perhaps convert values array , store array, or maybe using sqlite database, in can specify keys in 1 column, , corresponding value in , run select statement selects rows key in it.
Comments
Post a Comment