android - Save checkbox state with sharedpreferences -


in code added sharedpreferences , doesnt save anything. need when user ticks few checkboxes checkboxes should still same when app restarted or when button pressed.

here code:

public class myactivity3 extends activity { private textview tv; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_my3);     button m = (button) findviewbyid(r.id.button3);     tv = (textview) findviewbyid(r.id.textviewcat);     typeface typeface = typeface.createfromasset(getassets(), "bebasneue bold.ttf");     tv.settypeface(typeface);          sharedpreferences sharedpreferences=getsharedpreferences("ticks", context.mode_private);     sharedpreferences.getstring("name","");      //listview below! still needs check checkbox onitemclick , save state on exit , pressed!     string listarray[] = new string[] { "all", "friends & family", "sports", "outside",             "at school", "fitness", "photography", "food", "beach", "money" };     listview listview = (listview) findviewbyid(r.id.listview);     list<hashmap<string, string>> alist = new arraylist<hashmap<string, string>>();     (int = 0; <= listarray.length - 1; i++) {        hashmap<string, string> hm = new hashmap<string, string>();         hm.put("title", listarray[i]);         alist.add(hm);     }     string[] sfrm = { "title"};     int[] sto = { r.id.title};     simpleadapter adapter = new simpleadapter(getbasecontext(), alist,             r.layout.row_layout, sfrm, sto);    listview.setadapter(adapter);    listview.setonitemclicklistener(new adapterview.onitemclicklistener() {         @override        public void onitemclick(adapterview<?> arg0, view view,                                int position, long id) {            switch (position) {            }        }    }); }  public void save(view view) {         sharedpreferences sharedpreferences=getsharedpreferences("ticks", context.mode_private);     sharedpreferences.editor editor=sharedpreferences.edit();     editor.putstring("key","");     editor.commit(); } @override public void onbackpressed() {     super.onbackpressed();     overridependingtransition(r.anim.animation8, r.anim.animation7);     } } 

here's .xml checkbox has onclick="save" :

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="5dip" >  <com.mr.brd.customtextview     android:id="@+id/title"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_centervertical="true"     android:text="country name"     android:textcolor="#ffffff"     android:textsize="48sp" />  <checkbox android:id="@+id/chk"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:button="@drawable/custom_checkbox_design"     android:layout_centervertical="true"     android:layout_alignparentright="true"     android:layout_alignparentend="true"     android:onclick="save"/>   </relativelayout> 

am doing wrong? followed tutorial on sharedpreferences not getting somehow...

you not saving nothing on shared preference:

editor.putstring("key",""); 

always save empty string. , on oncreate not using retrieved string , must use same key.

sharedpreferences.getstring("name",""); 

you must change "name" "key".

also not catching event when checked state change on checkbox. in part of code, must set oncheckedchangelistener checkboxes, because setonitemclicklistener fired when 1 view of listview selected, not when checkbox selected or deselected.

modify oncreate activity method, , remove save method:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_my3);     button m = (button) findviewbyid(r.id.button3);     tv = (textview) findviewbyid(r.id.textviewcat);     typeface typeface = typeface.createfromasset(getassets(), "bebasneue bold.ttf");     tv.settypeface(typeface);      string[] listarray = new string[] { "all", "friends & family", "sports", "outside", "at school", "fitness", "photography", "food", "beach", "money" };     sharedpreferences sharedpreferences = getsharedpreferences("status", mode_private);      boolean[] checkedstatus = new boolean[listarray.length];     ( int index = 0; index < checkedstatus.length; index++)         checkedstatus[index] = sharedpreferences.getboolean(integer.tostring(index), false);      listview listview = (listview) findviewbyid(r.id.listview);     myadapter adapter = new myadapter(this, r.layout.row_layout, listarray, checkedstatus);     listview.setadapter(adapter); } 

and create myadapter class:

public class myadapter extends arrayadapter<string> implements compoundbutton.oncheckedchangelistener{  string[] values; boolean[] checkedstatus;  public myadapter(context context, int resource, string[] values, boolean[] checkedstatus) {     super(context, resource, values);      this.values = values;     this.checkedstatus = checkedstatus; }  @override public int getcount() {     return values.length; }  @override public string getitem(int position) {     return values[position]; }  @override public long getitemid(int position) {     return 0; }  @override public view getview(int position, view convertview, viewgroup parent) {     view view = convertview;     if(view == null){         layoutinflater inflater = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service);         view = inflater.inflate(r.layout.row_layout, null);     }      textview textview = (textview) view.findviewbyid(r.id.title);     textview.settext(values[position]);      checkbox box = (checkbox) view.findviewbyid(r.id.chk);     box.settag(position);     box.setoncheckedchangelistener(this);     box.setchecked(checkedstatus[position]);      return view; }  @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {     integer index = (integer) buttonview.gettag();     checkedstatus[index] = ischecked;     string key = index.tostring();      sharedpreferences sharedpreferences=getcontext().getsharedpreferences("status", context.mode_private);     sharedpreferences.editor editor=sharedpreferences.edit();     editor.putboolean(key,ischecked);     editor.apply(); } 

}

also, modify checkbox declaration on .xml file:

<textview     android:id="@+id/title"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_centervertical="true"     android:text="country name"     android:textcolor="#ffffff"     android:textsize="48sp" />  <checkbox android:id="@+id/chk"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:button="@drawable/custom_checkbox_design"     android:layout_centervertical="true"     android:layout_alignparentright="true"     android:layout_alignparentend="true"/> 

removing,

android:onclick="save" 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -