android - Listview with button OnItemClickListener is not calling -
i create custom listview
textview
, 2 imagebutton
. onitemclicklistner
not calling. here code
police_station_list = (listview) findviewbyid(r.id.police_station_listview_xml); adapter = new arrayadapter<string>(this, r.layout.coustome_listview, r.id.district_listview_xml1, police_station_name); psadapter = new police_station_adapter(this); police_station_list.setadapter(psadapter); police_station_list.setonitemclicklistener(psadapter);
here custom arrayadapter
class:
public class police_station_adapter extends arrayadapter<string> implements onitemclicklistener{ context context; public police_station_adapter(context context) { //super(context, r.layout.police_station_listview, r.id.police_station_listview_textview, police_station_name); super(context, r.layout.police_station_listview, police_station_name); this.context = context; } private class viewholder { textview tv; imagebutton mobile, phone; public viewholder(view v) { tv = (textview) v.findviewbyid(r.id.police_station_listview_textview); mobile = (imagebutton) v.findviewbyid(r.id.police_station_listview_imagebutton_mobile); phone = (imagebutton) v.findviewbyid(r.id.police_station_listview_imagebutton_phone); } } viewholder holder; @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; if(v == null) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); v = inflater.inflate(r.layout.police_station_listview, parent, false); holder = new viewholder(v); v.settag(holder); } else { holder = (viewholder) v.gettag(); } holder.tv.settext(police_station_name[position]); holder.mobile.settag(position); holder.mobile.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { new message(context, police_station_name[(integer) v.gettag()]+" button", 500); } }); return v; } @override public void onitemclick(adapterview<?> parent, view view, final int position, long id) { new message(context, "item clicked", 500); //toast message }
}
here custom listview
xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_horizontal" android:orientation="horizontal" > <textview android:id="@+id/police_station_listview_textview" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="8" android:textsize="16sp" android:layout_marginright="10dp" android:textappearance="?android:attr/textappearancelarge" /> <imagebutton android:id="@+id/police_station_listview_imagebutton_mobile" android:layout_gravity="center_vertical" android:layout_width="27dp" android:layout_height="30dp" android:layout_weight="1" android:layout_marginright="15dp" android:background="@drawable/mobile_icon" /> <imagebutton android:id="@+id/police_station_listview_imagebutton_phone" android:layout_gravity="center_vertical" android:layout_width="27dp" android:layout_height="30dp" android:layout_weight="1" android:layout_marginright="10dp" android:background="@drawable/telephone_icon" /> </linearlayout>
it's seems can add onclikclistner
textview doesn't show pressed animation. may not appropriate solution. need proper way setonitemclicklistner
.
you need set onitemclicklistener in activity. change
police_station_list.setonitemclicklistener(psadapter); police_station_list.setonitemclicklistener(this);
remove implementation of onitemclicklistener adapter. check tutorial.
Comments
Post a Comment