Android: Delete item from Listview that was populated using Parse in Custom Adapter -
i trying delete item in parse.com using button in listview. listview populated using custom adapter class , executing onclick delete it. great!
update: delete module modulecode equal position's modulecode
this part of adapter trying work error parseexception e is: 04-09 19:33:05.408 10425-10425/com.yupo.dominic.yupo e/error﹕ java.lang.illegalargumentexception: invalid type parseobject: class com.yupo.dominic.yupo.module
button moduledeletebutton = (button)view.findviewbyid(r.id.moduledeletebutton); moduledeletebutton.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { parsequery<parseobject> query = new parsequery<parseobject>("module"); query.whereequalto("objectid", getitem(position)); query.findinbackground(new findcallback<parseobject>() { public void done(list<parseobject> module, parseexception e) { if (e == null) { // iterate on messages , delete them for(parseobject delete : module) { delete.deleteinbackground(); } } else { log.e("error", e.getmessage()); } } }); toast.maketext(v.getcontext(), "deleted", toast.length_long).show(); } });
this full adapter:
package com.yupo.dominic.yupo; import java.util.arraylist; import java.util.list; import android.content.context; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.button; import android.widget.textview; import android.widget.toast; import com.parse.findcallback; import com.parse.parseexception; import com.parse.parseobject; import com.parse.parsequery; public class customlistviewadapter extends baseadapter { // declare variables context mcontext; layoutinflater inflater; private list<module> modulelist = null; private arraylist<module> arraylist; public customlistviewadapter(context context,list<module> modulelist) { mcontext = context; this.modulelist = modulelist; inflater = layoutinflater.from(mcontext); this.arraylist = new arraylist<module>(); this.arraylist.addall(modulelist); } public class viewholder { textview modulecode; textview modulename; textview moduleuni; } @override public int getcount() { return modulelist.size(); } @override public module getitem(int position) { return modulelist.get(position); } @override public long getitemid(int position) { return position; } public view getview(final int position, view view, viewgroup parent) { final viewholder holder; if (view == null) { holder = new viewholder(); view = inflater.inflate(r.layout.module_list_item, null); // locate textviews in listview_item.xml holder.modulecode = (textview) view.findviewbyid(r.id.modulecodetextview); holder.modulename = (textview) view.findviewbyid(r.id.modulenametextview); holder.moduleuni = (textview) view.findviewbyid(r.id.moduleunitextview); view.settag(holder); } else { holder = (viewholder) view.gettag(); } // set results textviews holder.modulecode.settext(modulelist.get(position).getmodulecode()); holder.modulename.settext(modulelist.get(position).getmodulename()); holder.moduleuni.settext(modulelist.get(position).getmoduleuni()); // listen listview item click button moduledeletebutton = (button)view.findviewbyid(r.id.moduledeletebutton); moduledeletebutton.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { parsequery<parseobject> query = new parsequery<parseobject>("module"); query.whereequalto("objectid", getitem(position)); query.findinbackground(new findcallback<parseobject>() { public void done(list<parseobject> module, parseexception e) { if (e == null) { // iterate on messages , delete them for(parseobject delete : module) { delete.deleteinbackground(); } } else { log.e("error", e.getmessage()); } } }); toast.maketext(v.getcontext(), "deleted", toast.length_long).show(); } }); return view; } }
here module class if needed:
package com.yupo.dominic.yupo; public class module { private string modulename; private string modulecode; private string moduleuni; public string getmodulename() { return modulename; } public void setmodulename(string modulename) { this.modulename = modulename; } public string getmodulecode() { return modulecode; } public void setmodulecode(string modulecode) { this.modulecode = modulecode; } public string getmoduleuni() { return moduleuni; } public void setmoduleuni(string moduleuni) { this.moduleuni = moduleuni; } }
managed work out. changed query.whereequalto("objectid", getitem(position));
to
query.whereequalto("modulecode", getitem(position).getmodulecode());
not wanted works :)