android - Deleting custom objects from a listview within a fragment using onClick XML -
i'm still learning lot programming. i've set own listview item layout display custom object. within custom listview layout, there's image button has onclick method associated it.
i've realised onclick passed parent activity, i'm not sure how remove listview item arraylist of custom object within fragment.
i'm not explaining myself well, here code snippets go it.
image button xml within custom listview layout:
<imagebutton android:layout_width="0dp" android:layout_height="match_parent" android:src="@drawable/deleteitem" android:scaletype="fitcenter" android:background="@null" android:adjustviewbounds="true" android:layout_weight="0.15" android:layout_marginend="5dp" android:id="@+id/deleteitembutton" android:onclick="delitem"/>
onclick method in parent activity:
public void delitem(view v) { _itemobject itemtoremove = (_itemobject) v.gettag(); itemlist_fragment itemlistclass = new itemlist_fragment(); itemlistclass.removeitem(itemtoremove); }
removeitem method found in fragment:
public void removeitem(_itemobject itemtoremove) { adapter.remove(itemtoremove); adapter.notifydatasetchanged(); }
this error received says "adapter" in fragment null. earlier initialised when fragment created. appreciated.
you don't need make complex. remove android:onclick="delitem"
view , attach listener in getview()
. initialize deleteitembutton
, setonclicklistener()
-
deleteitembutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { _itemobject itemtoremove = (_itemobject) v.gettag(); remove(itemtoremove); notifydatasetchanged(); } });
Comments
Post a Comment