android - Deleting items in ListView using intent -
my problem im trying delete items listview , have button in customadapter.
im setting button onclicklistener , try pass item name main activity using intent.
in main when intent named "deleteproduct" received method deleteproduct called , in method im trying pass database product name delete.
my customadapter:
private dbitemdeletelistener deletelistener; customadapter(context context, arraylist<product> productnames,dbitemdeletelistener deletelistener) { super(context,r.layout.custom_list_row ,productnames); this.deletelistener = deletelistener; } final product singleproduct=getitem(position); final textview productname=(textview)customview.findviewbyid(r.id.productname); final button checkbutton = (button)customview.findviewbyid(r.id.checkbutton); final button deletebutton = (button)customview.findviewbyid(r.id.deletebutton); deletebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string product=singleproduct.get_productname(); deletelistener.delete(product); } });
my main:
dbitemdeletelistener deletelistener; arrayadapter<product> adapter; arraylist<product> productnames=new arraylist<>(); dbhandler dbhandler; @override public void delete(string productid){ dbhandler.deleteproduct(productid); }
and dbhandler:
public void deleteproduct(string productname){ sqlitedatabase db=getwritabledatabase(); db.execsql("delete " +table_products+ " "+ column_productname+ "=\" "+ productname +"\";"); }
also im getting message in logcat when click delete button:
process: com.example.olev.shoppinglist, pid: 1836 java.lang.nullpointerexception: attempt invoke interface method 'void com.example.olev.shoppinglist.dbitemdeletelistener.delete(java.lang.string)' on null object reference @ com.example.olev.shoppinglist.customadapter$3.onclick(customadapter.java:80) @ android.view.view.performclick(view.java:4756)
you shouldn't need use intent delete list item. instead, use observer pattern. first, create interface:
public interface dbitemdeletelistener{ public void delete(string productid); }
implement interface in activity (your activity not greatest place implement, since it's you're doing deleting, i'll stick that):
public class myactivity extends activity implements dbitemdeletelistener{ ... @override public void delete(string productid){ dbhandler.deleteproduct(productid); } }
then, pass instance of class implements listener adapter's constructor:
public customadapter extends whateveradapter{ private dbitemdeletelistener deletelistener; public myadapter(dbitemdeletelistener deletelistener){ this.deletelistener = listener; } }
make sure use version of constructor when create adapter.
then, instead of onclicklistener sending intent:
deletebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //get product name , use listener delete ... deletelistener.delete(productid); notifydatasetchanged(); } }
but if still plan on using intents reason:
http://developer.android.com/reference/android/app/activity.html#onnewintent(android.content.intent)
this called activities set launchmode "singletop" in package, or if client used flag_activity_single_top flag when calling startactivity(intent).
what i'm guessing happening instead of getting current instance of main activity, you're creating new instance. therefore, should doing same check in oncreate() instead.
so can either set activity's launch mode call onnewintent:
<activity android:launchmode="singletop" ...> .... </activity>
and/or add flag intent:
intent.addflags(intent.flag_activity_single_top);
or, maybe easier, can move delete call oncreate:
@override protected void oncreate(bundle savedinstancestate) { ... //other oncreate() stuff if(getintent() != null && getintent().hasextra("deleteproduct")){ if (intent.getstringextra("deleteproduct").equals("deleteproduct")) { deleteproduct(); } } }
also, should checking
intent.getstringextra("deleteproduct").equals("deleteproduct")
not ".equals("deleteprocust") in sample code, assume that's typo.
Comments
Post a Comment