android - how save checkbox value with shared preferences? -


this question has answer here:

hy everybody, don't know why code not save checkbox checked value; if check checkbox , after click on tab , comeback previous tab, checkbox not selected...i hope find error in code!

ide me: fatal exception: main java.lang.nullpointerexception @ line ".oncreateview(holder.chkbox.setchecked(true);"

why?

thanks in advance!

adapter class:

public abstract class planetadapter extends arrayadapter<planet> implements compoundbutton.oncheckedchangelistener  {    private list<planet> planetlist;   private context context;   arraylist<birra> objects;     public  planetadapter(list<planet> planetlist, context context) {       super(context, r.layout.single_listview_item, planetlist);       this.planetlist = planetlist;       this.context = context;   }         public  class planetholder  {       public textview planetname;       public textview distview;       public textview valuta;       public checkbox chkbox;       public edittext edit;       public string quantità;      }    @override   public view getview(int position, view convertview, viewgroup parent) {            view row = convertview;       planetholder holder = null;       if (row == null) {           layoutinflater inflater = ((activity) context).getlayoutinflater();           row = inflater.inflate(r.layout.single_listview_item, parent, false);           holder = new planetholder();           holder.planetname = (textview) row.findviewbyid(r.id.name);           holder.distview = (textview) row.findviewbyid(r.id.dist);           holder.valuta = (textview) row.findviewbyid(r.id.valuta);           holder.chkbox = (checkbox) row.findviewbyid(r.id.chk_box);           holder.edit = (edittext) row.findviewbyid(r.id.edittext);           holder.edit.setvisibility(view.gone);           holder.edit.setenabled(false);           row.settag(holder);       } else {           holder = (planetholder) row.gettag();       }        final planet p = planetlist.get(position);         final planetholder finalholder = holder;       if(p.ischeckboxchecked ())       {           finalholder.chkbox.setchecked(true);       }       /*sharedpreferences preferences = getcontext().getsharedpreferences("materialtabs", android.content.context.mode_private);       boolean mcheckboxvalue = preferences .getboolean("checkbox_value", false);        if (mcheckboxvalue) {            holder.chkbox.setchecked(true);       } else {           holder.chkbox.setchecked(false);       }*/         holder.chkbox.setonclicklistener(new view.onclicklistener() {           @override           public void onclick(view v) {               if (finalholder.chkbox.ischecked()) {                   finalholder.edit.setvisibility(view.visible);                   finalholder.edit.setenabled(true);                   sharedpreferences preferences = getcontext().getsharedpreferences("materialtabs", android.content.context.mode_private);                  /* sharedpreferences.editor editor = preferences.edit();                   editor.putboolean("showactivity", finalholder.chkbox.ischecked());                   editor.commit();*/                  // boolean value= true;                   sharedpreferences.editor meditor = preferences .edit();                   meditor.putboolean("checkbox_value", finalholder.chkbox.ischecked());                    meditor.commit();                    finalholder.edit.addtextchangedlistener(new textwatcher() {                       @override                       public void beforetextchanged(charsequence s, int start, int count, int after) {                       }                        @override                       public void ontextchanged(charsequence s, int start, int before, int count) {                       }                        @override                       public void aftertextchanged(editable s) {                           p.setquantità(finalholder.edit.gettext().tostring().trim());                           sharedpreferences preferences = getcontext().getsharedpreferences("project_name", android.content.context.mode_private);                           sharedpreferences.editor editor = preferences.edit();                           editor.putstring("key", finalholder.edit.gettext().tostring().trim());                            editor.commit();                        }                   });               } else {                   finalholder.edit.setvisibility(view.gone);                   finalholder.edit.setenabled(false);                   finalholder.edit.settext(null);                }            }       });       holder.planetname.settext(p.getname());       holder.distview.settext("" + p.getdistance());       holder.valuta.settext(""+p.getvaluta());       holder.chkbox.setchecked(p.isselected());       holder.chkbox.settag(p);       holder.edit.setenabled(false);            return row;   }     arraylist<planet> getbox() {       arraylist<planet> box = new arraylist<planet>();       (planet p : planetlist) {           if (p.selected)               box.add(p);       }       return box;   }   } 

fragment:

public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment      viewgroup rootview = (viewgroup) inflater.inflate(r.layout.fragment_list2, container, false);     sharedpreferences preferences = getcontext().getsharedpreferences("materialtabs", android.content.context.mode_private);       // preferences.getboolean("showactivity", false);        //preferences.getstring("key", null);     boolean mcheckboxvalue = preferences .getboolean("checkbox_value", false);     if (mcheckboxvalue) {         holder.chkbox.setchecked(true);     } else {         holder.chkbox.setchecked(false);     }          button mbutton = (button) rootview.findviewbyid(r.id.button);     mbutton.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {         showresult(v);           }     });     //return inflater.inflate(r.layout.fragment_list2, container, false);     return rootview; } 

planet:

class planet {      string name;     int distance;     string quantità;     string valuta;     boolean selected = false;     boolean mcheckboxstate;       public planet(string name, int distance, string valuta) {         super();         this.name = name;         this.distance = distance;         this.valuta = valuta;     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public int getdistance() {         return distance;     }      public void setdistance(int distance) {         this.distance = distance;     }      public boolean isselected() {         return selected;     }      public void setselected(boolean selected) {         this.selected = selected;     }      public string getquantità() {         return quantità;     }      public void setquantità(string quantità) {         this.quantità = quantità;     }     public string getvaluta() {         return valuta;     }      public void setvaluta(string valuta) {         this.valuta = valuta;     }       public boolean ischeckboxchecked () { return mcheckboxstate; }     private void setcheckboxcheck(boolean state){ mcheckboxstate = state; }  } 

the fragment views refreshed each time better option instead of storing in preferences make use of item (object) used create value ex in adapter carry out operation final planet p = planetlist.get(position);

  final planetholder finalholder = holder;    //set check box if checked   if(p.ischeckboxchecked)   {    finalholder.chkbox.setchecked(true);   }     holder.chkbox.setonclicklistener(new view.onclicklistener() {       @override       public void onclick(view v) {           if (finalholder.chkbox.ischecked()) {               finalholder.edit.setvisibility(view.visible);               finalholder.edit.setenabled(true);               sharedpreferences preferences = getcontext().getsharedpreferences("materialtabs", android.content.context.mode_private);              /* sharedpreferences.editor editor = preferences.edit();               editor.putboolean("showactivity", finalholder.chkbox.ischecked());               editor.commit();*/              // boolean value= true;               sharedpreferences.editor meditor = preferences .edit();               meditor.putboolean("checkbox_value", finalholder.chkbox.ischecked());               meditor.commit();                finalholder.edit.addtextchangedlistener(new textwatcher() {                   @override                   public void beforetextchanged(charsequence s, int start, int count, int after) {                   }                    @override                   public void ontextchanged(charsequence s, int start, int before, int count) {                   }                    @override                   public void aftertextchanged(editable s) {                    //set item been checked                   p.setcheckboxcheck(true);                     }               });           } else {               finalholder.edit.setvisibility(view.gone);               finalholder.edit.setenabled(false);               finalholder.edit.settext(null);            }        }   }); 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -