java - OnItemClickListener doesn't work but OnLongItemClickListener works in Custom Listview -
i have custom listview
custom adapter. want click on items of listview
, something. onitemclicklistener
not works. implemented onlongitemclicklistener
and works perfectly.
mainactivity
public class mainactivity extends activity { arraylist<product> products = new arraylist<product>(); adapter listviewadapter; //custom adapter object listview listview; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (listview) findviewbyid(r.id.lvmain); listview.setlongclickable(true); listviewadapter = new adapter(this, products); listview.setadapter(listviewadapter); listview.setonitemlongclicklistener(new onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { //this works toast.maketext(getapplicationcontext(), "long pressed", toast.length_short).show(); return false; } }); listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { //does not work toast.maketext(getapplicationcontext(), " pressed", toast.length_short).show(); } }); }
update custom adapter adapter
public class adapter extends baseadapter { context ctx; layoutinflater linflater; arraylist<product> objects; textview itemname,itemprice; adapter(context context, arraylist<product> products) { ctx = context; objects = products; linflater = (layoutinflater) ctx .getsystemservice(context.layout_inflater_service); } @override public int getcount() { return objects.size(); } @override public object getitem(int position) { return objects.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view view = convertview; if (view == null) { view = linflater.inflate(r.layout.item, parent, false); } product p = getproduct(position); itemname= ((textview) view.findviewbyid(r.id.tvdescr)); itemname.settext(p.name); itemprice=((textview) view.findviewbyid(r.id.tvprice)); itemprice.settext(p.price + ""); checkbox cbbuy = (checkbox) view.findviewbyid(r.id.cbbox); cbbuy.setoncheckedchangelistener(mycheckchanglist); cbbuy.settag(position); cbbuy.setchecked(p.selected); view.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub } }); view.setonlongclicklistener(new onlongclicklistener() { @override public boolean onlongclick(view v) { // todo auto-generated method stub return false; } }); return view; } product getproduct(int position) { return ((product) getitem(position)); } arraylist<product> getbox() { arraylist<product> selected = new arraylist<product>(); (product p : objects) { if (p.selected) selected.add(p); } return selected; } oncheckedchangelistener mycheckchanglist = new oncheckedchangelistener() { public void oncheckedchanged(compoundbutton buttonview,boolean ischecked) { getproduct((integer) buttonview.gettag()).selected = ischecked; } };
}
custom listview item.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:descendantfocusability="blocksdescendants"> <checkbox android:id="@+id/cbbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" > </checkbox> <linearlayout android:id="@+id/linearlayout1" android:layout_height="wrap_content" android:orientation="vertical" > <textview android:id="@+id/tvdescr" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" > </textview> </linearlayout>
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <listview android:id="@+id/lvmain" android:layout_width="match_parent" android:layout_height="0dp" android:longclickable="true"> </listview>
to text views , check box
android:focusable="false"
Comments
Post a Comment