android - How to set multiple click actions on ListView's item -
i added 2 imageviews on listview's each item. want add these imageviews onclick actions. add android:onclick="action"
imageview's on list_item.xml.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/listitem" android:orientation="horizontal" > <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_marginleft="5dp" android:background="@drawable/point" /> <textview android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_marginleft="15dp" android:layout_torightof="@+id/imageview1" android:textcolor="#000000" /> <imageview android:id="@+id/btn_youtube" android:layout_width="30dp" android:layout_height="30dp" android:clickable="true" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:layout_marginright="10dp" android:background="@drawable/youtube" android:onclick="myclickhandler2" /> <imageview android:id="@+id/btn_music" android:layout_width="30dp" android:layout_height="30dp" android:clickable="true" android:layout_alignparenttop="true" android:layout_marginright="10dp" android:layout_toleftof="@+id/btn_youtube" android:background="@drawable/musicstore" android:onclick="myclickhandler1" /> </relativelayout>
then add myclickhandler1 , myclickhandler2 functions class. when clicked btn_music, myclickhandler1 function called , on btn_youtube same. can not position of listview's item. must position of row number of list item contains clicked imageview(btn_music or btn_youtube). class codes:
public class tab2 extends activity implements adapterview.onitemclicklistener{ private layoutinflater minflater; private vector<rowdata> data; rowdata rd; context context ; bitmap bitmap; public static string title[] = { "song1", "son2", "song3"}; public static integer[] imgid = {r.drawable.play, r.drawable.play, r.drawable.play}; @override protected void oncreate(bundle savedinstancestate) { setcontentview(r.layout.tab2); super.oncreate(savedinstancestate); intent intent = getintent(); string _status = (string) intent.getserializableextra("status"); minflater = (layoutinflater) getsystemservice(activity.layout_inflater_service); data = new vector<rowdata>(); (int = 0; < title.length; i++) { try { rd = new rowdata(i, title[i]); } catch (parseexception e) { e.printstacktrace(); } data.add(rd); } customadapter adapter = new customadapter(this, r.layout.activity_list_view, r.id.list, data); android.widget.listview list = (android.widget.listview) findviewbyid(r.id.list); list.setadapter(adapter); list.setonitemclicklistener(this); } private class rowdata { protected int mid; protected string mtitle; rowdata(int id, string title) { mid = id; mtitle = title; } @override public string tostring() { return mid + " " + mtitle; } } private class customadapter extends arrayadapter<rowdata> { public customadapter(context context, int resource, int textviewresourceid, list<rowdata> objects) { super(context, resource, textviewresourceid, objects); } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; textview title = null; imageview i11 = null; rowdata rowdata = getitem(position); if (null == convertview) { convertview = minflater .inflate(r.layout.activity_list_view, null); holder = new viewholder(convertview); convertview.settag(holder); } holder = (viewholder) convertview.gettag(); title = holder.gettitle(); title.settext(rowdata.mtitle); return convertview; } private class viewholder { private view mrow; private textview title = null; private imageview i11 = null; public viewholder(view row) { mrow = row; } public textview gettitle() { if (null == title) { title = (textview) mrow.findviewbyid(r.id.title); } return title; } } } public void myclickhandler1(view v) { relativelayout vwparentrow = (relativelayout)v.getparent(); listview list = (listview)findviewbyid(r.id.list); string uri[]={"http://www.youtube.com/watch?v=rmltod1jcgi", "http://www.youtube.com/watch?v=928obph1wo0", "http://www.youtube.com/watch?v=yfuauifjnjg"}; int position = vwparentrow.indexofchild(v); startactivity(new intent(intent.action_view,uri.parse(uri[position-2]))); } public void myclickhandler2(view v) { relativelayout vwparentrow = (relativelayout)v.getparent(); string uri[]={"http://www.youtube.com/watch?v=1ogtxqidstu", "http://www.youtube.com/watch?v=imxdnpi_c98", "http://www.youtube.com/watch?v=kovhn6eo4xg"}; listview list = (listview)findviewbyid(r.id.list); int position = vwparentrow.indexofchild(v); startactivity(new intent(intent.action_view,uri.parse(uri[position-2]))); } @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub } }
again want problem functions @ bottom of tab2 class. there no stack, no error. can not control listitem's imageview clicked. thanks.
i have asked question. need implement view holder: see post: android gridview clickable imageview
you need re-structure adapter. see accepted answer in above link.
Comments
Post a Comment