android - onClickListener for ImageView within ListView managed by ArrayAdapter -
i have listview
content handled custom arrayadapter
. layout i'm using has 1 textview
, 1 imageview
.
layout:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <textview android:id="@+id/string" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/default" android:textsize="20sp"/> <imageview android:id="@+id/delete_row" android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/ic_cancel" android:layout_alignparentend="true" android:layout_alignparentright="true" android:contentdescription="@string/delete_row"/> </relativelayout>
main java class:
listview listview = (listview) findviewbyid(r.id.listview); arrayadapter<string> arrayadapter = new stringlist(this, strings); listview.setemptyview(findviewbyid(r.id.empty)); listview.setadapter(arrayadapter);
now declare onclicklistener
hole item, quite easy:
listview.setonitemclicklistener((parent, view, position, id) -> { .... });
but know want have onclicklistener
imageview
,
i don't want put onclicklistener
in custom arrayadapter
file, because when clicking on image, there should happen lot of things (not removing row).
so how can set onclicklistener
imageview
within listview
, managed arrayadapter
?
inside adapter getview implement onclicklistener image view
@override public view getview(int position, view convertview, viewgroup parent) { if(convertview == null){ convertview = inflater.inflate(r.layout.list_row, parent, false); } imageview imageview = (imageview) convertview.findviewbyid(r.id.imageview); imageview.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { // staff } }); return convertview; }
Comments
Post a Comment