android - How can I delete a determined row in Sqlite? -
i have listview
. add single row in sqlite database checking it.(checkbox
) @ same time need delete row, i've unchecked. need id of row stored in table, due can delete row. how can ? checkbox
changing code in adapter
.
public class mainvacancyadapter extends arrayadapter { private list<vacancymodel> vacancymodellist; private int resource; private layoutinflater inflater; arraylist<boolean> isselected; sqlhelper sqlhelper; public mainvacancyadapter(context context, int resource, list<vacancymodel> objects) { super(context, resource, objects); vacancymodellist = objects; this.resource = resource; sqlhelper = new sqlhelper(getcontext()); inflater = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service); isselected = new arraylist<>(vacancymodellist.size()); (int = 0; < vacancymodellist.size(); i++) { isselected.add(false); } } //finding textviews , inserting data them @override public view getview(final int position, view convertview, final viewgroup parent) { viewholder holder = null; if (convertview == null) { holder = new viewholder(); convertview = inflater.inflate(resource, null); holder.tvheader = (textview) convertview.findviewbyid(r.id.tvheader); holder.tvprofilename = (textview) convertview.findviewbyid(r.id.tvprofilename); holder.tvsalary = (textview) convertview.findviewbyid(r.id.tvsalary); holder.tvpostcr = (textview) convertview.findviewbyid(r.id.tvpostcr); //holder.tvbody = (textview) convertview.findviewbyid(r.id.tvbody); holder.cbxfav = (checkbox) convertview.findviewbyid(r.id.cbxfav); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); holder.cbxfav.setoncheckedchangelistener(null); } holder.tvheader.settext(vacancymodellist.get(position).getheader()); holder.tvprofilename.settext(vacancymodellist.get(position).getprofilename()); holder.tvsalary.settext(vacancymodellist.get(position).getsalary()); holder.tvpostcr.settext(vacancymodellist.get(position).getpostcreated()); //holder.tvbody.settext(vacancymodellist.get(position).getbody()); holder.cbxfav.setchecked(isselected.get(position)); holder.cbxfav.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { //inserting data favourite table in sql isselected.set(position, true); sqlhelper.createfavouritetable(vacancymodellist.get(position)); //toast.maketext(getcontext(), "added favourite", toast.length_short).show(); } else { isselected.set(position, false); sqlhelper.deletefromfavouritedatabase(position); //toast.maketext(getcontext(), "deleted favourite", toast.length_short).show(); } } }); return convertview; } static class viewholder { textview tvheader; textview tvprofilename; textview tvsalary; textview tvpostcr; textview tvbody; checkbox cbxfav; } }
method in sqlhelper class
public void deletefromfavouritedatabase(int rowid) { sqlitedatabase db = this.getwritabledatabase(); db.delete(favourite.table_name, favourite.key_id + " = " + rowid, null); db.close(); }
place column of id
in vacancymodel
, bind on delete
int id = vacancymodellist.get(position).getid() sqlhelper.deletefromfavouritedatabase(id);
Comments
Post a Comment