android - CustomListView OnItemClickListener not working -
i used local database fetch records , create custom list view using that. custom list view gets displayed perfectly. problem onitemclicklistener. doesn't on click. aim send position of clicked item activity. implemented it's not working. screenshot of list view - https://www.dropbox.com/s/pz83i162sxdv2b0/untitled.png?dl=0
here mainactivity.java :
public class mainactivity extends actionbaractivity { listview lv; textview tv1,tv2,tv3; arraylist<string> a=new arraylist<string>(); string mydata,name,name1; public string[] s1 = new string[50]; public int[] img = {r.drawable.rty, r.drawable.sf, r.drawable.rty}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv1=(textview)findviewbyid(r.id.textview); lv = (listview) findviewbyid(r.id.listview); new mydata().execute(); lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent in = new intent(mainactivity.this, listclick.class); in.putextra("position", position); startactivity(in); } }); } public void abc(view v) { intent in=new intent(mainactivity.this,webform.class); startactivity(in); } public class mydata extends asynctask<string,string,string> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected void onpostexecute(string s) { super.onpostexecute(s); customadapter cad = new customadapter(mainactivity.this, s1, img); lv.setadapter(cad); } @override protected string doinbackground(string... params) { getdata(); return null; } } public void getdata() { try { httpclient httpclient=new defaulthttpclient(); httppost httppost=new httppost("http://10.0.2.2/abcd.php"); httpresponse response=httpclient.execute(httppost); httpentity httpentity=response.getentity(); inputstream is=httpentity.getcontent(); bufferedreader reader=new bufferedreader(new inputstreamreader(is,"utf-8"),8); stringbuilder strbuilder=new stringbuilder(); string line=null; while ((line=reader.readline())!=null) { strbuilder.append(line); } is.close(); mydata=strbuilder.tostring(); jsonarray obj=new jsonarray(mydata); for(int i=0;i<obj.length();i++) { jsonobject obj1=obj.getjsonobject(i); a.add(i,obj1.getstring("name")); } string[] s = new string[a.size()]; s=a.toarray(s); s1 = s; } catch (exception e) { } } }
here second activity listclick.java
public class listclick extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_listclick); intent in=getintent(); string s1=in.getstringextra("position"); } }
here customlist.xml (layout file)
<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff9200" android:clickable="true"> <tablerow> <imageview android:layout_width="70dp" android:layout_height="70dp" android:background="@drawable/sf" android:id="@+id/imageview" /> <textview android:layout_height="70dp" android:layout_width="fill_parent" android:textsize="20dp" android:fitssystemwindows="true" android:textstyle="bold" android:layout_gravity="center" android:gravity="center" android:paddingleft="20dp" android:text="codewars 3.0" android:id="@+id/textview" /> </tablerow> </tablelayout>
here customadapter.java
public class customadapter extends arrayadapter<string>{ context c1; string s1[]; int s2[]; customadapter(context c,string s[],int s3[]) { super(c,r.layout.listcustom,s); this.c1=c; this.s1=s; this.s2=s3; } @override public view getview(int position, view v, viewgroup parent) { layoutinflater li=(layoutinflater) c1.getsystemservice(context.layout_inflater_service); v=li.inflate(r.layout.listcustom,null); textview tv=(textview)v.findviewbyid(r.id.textview); tv.settext(s1[position]); return v; } }
i think there 2 issues in code, first listcustom.xml has set:
android:clickable="true"
if going use onitemclicklistener() of listview don't need part. set clickable if going set view.onclicklistner ever item. remove line in xml.
the next line might cause issues below:
inflate(r.layout.listcustom,null)
you should inflate items in list this:
inflate(r.layout.listcustom, parent, false);
Comments
Post a Comment