java - set boolean value to Realm database when checkbox is checked -
i have realm database model want display in arecyclerview, every row in database model have string name,int age , boolean favourite,when try save boolean value in recyclerviewadapter using interface gjava.lang.nullpointerexception: attempt invoke virtual method 'void io.realm.realm.begintransaction()' on null object reference
this model
public class person extends realmobject implements{ @primarykey private string name; private string job; private boolean favourite; private string image; public authorinfo(string image, string job, string name, boolean favoutite) { this.image = image; this.job = job; this.name = name; this.favourite = favourite; } public authorinfo() { } public string getimage() { return image; } public void setimage(string image) { this.image = image; } public string getjob() { return job; } public void setjob(string job) { this.job = job; } public string getname() { return name; } public void setname(string name) { this.name = name; } public boolean isfavourite() { return favourite; } public void setfavourite(boolean favourite) { this.favourite = favourite; }
}
this interface
public interface itemclicklistener { void onitemclick(view view,int position);}
and recyclerview adapter
public class personadapter extends recyclerview.adapter<authoradapter.myviewholder> { private layoutinflater inflater; private realm mrealm; private realmresults<authorinfo> results; public personadapter(context context, realmresults<authorinfo> realmresults) { inflater = layoutinflater.from(context); results = realmresults; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = inflater.inflate(r.layout.custom_row, parent, false); myviewholder holder = new myviewholder(view); return holder; } @override public void onbindviewholder(myviewholder holder, final int position) { authorinfo current = results.get(position); holder.name.settext(current.getname()); holder.job.settext(current.getjob()); holder.setclicklistener(new itemclicklistener() { mrealm.begintransaction(); @override public void onitemclick(view view, int position) { checkbox chk= (checkbox) view; if (chk.ischecked()){ results.get(position).setfavourite(true); } else if (!chk.ischecked()){ results.get(position).setfavourite(false); } } mrealm.committransaction(); notifyitemchanged(position) }); } @override public int getitemcount() { return results.size(); } public static class myviewholder extends recyclerview.viewholder implements view.onclicklistener { textview name; textview job; checkbox checkbox; itemclicklistener itemclicklistener; public myviewholder(view itemview) { super(itemview); name = (textview) itemview.findviewbyid(r.id.tv_name); job = (textview) itemview.findviewbyid(r.id.tv_job); checkbox = (checkbox) itemview.findviewbyid(r.id.checkbox); checkbox.setonclicklistener(this); } public void setclicklistener(itemclicklistener ic) { this.itemclicklistener = ic; } @override public void onclick(view view) { this.itemclicklistener.onitemclick(view,getadapterposition()); } }
}
so , ideas?!
Comments
Post a Comment