java - sharedpreferences stringset value: How to remove in Android Studio.? -
i have unchecked checkbox(per item) remove . using sharedpreferences hashset concept. want pass arraylist value 1 page page. what mistake 1 me.
my code is:
adapterpage.java public static final string my_prefs_name = ""; @override public void onclick(view view) { boolean ischecked = mainholder.chekenitem.ischecked(); // arr=getresources().getstringarray( mainholder.txtenimgid.gettext().tostring()); // boolean ischecked = mainholder.chekenitem.ischecked(); int i; string itemid1 = mainholder.txtenimgid.gettext().tostring(); sharedpreferences prefs=view.getcontext().getsharedpreferences(my_prefs_name,context.mode_private); sharedpreferences.editor edit=prefs.edit(); set<string> set = new hashset<string>(); try { if (ischecked) { addmembers.add(itemid1); (j = 0; j < addmembers.size(); j++) { set.addall(addmembers); edit.putstringset("yourkey", set); edit.commit(); toast.maketext(view.getcontext(), "clicked on checkbox addmembers[pos] : " + addmembers.get(j) + " item id " + itemid1 , toast.length_long).show(); } // editor.putstring("key_name",itemid ); // editor.apply(); // } } else { for(int k=0;k<=addmembers.size();k++){ if(addmembers.get(k).equals(itemid1)){ // set.remove(addmembers); addmembers.remove(k); edit.remove(addmembers.get(k)); edit.commit(); break; } }
you making mistake here ...
(j = 0; j < addmembers.size(); j++) { //set.addall(addmembers);this add every time whole data of list set set.add(addmembers.get(j)); edit.putstringset("yourkey", set); edit.commit(); toast.maketext(view.getcontext(), "clicked on checkbox addmembers[pos] : " + addmembers.get(j) + " item id " + itemid1 , toast.length_long).show(); }
and here while removing change this... edit.remove("yourkey");
Comments
Post a Comment