android - listview setonitemclicklistener doesn't work even me apply almost all solution available on the stack over flow -
customadapter.class
public class customadapter extends arrayadapter<string> { context context; int[] images; string[] titlearray; string[] descriptionarrayl; public customadapter(context c,string[] memetitles,int[] imgs,string[] descriptionarrayl) { super(c,r.layout.row,r.id.ettitle,memetitles); this.context=c; this.images = imgs; this.titlearray = memetitles; this.descriptionarrayl=descriptionarrayl; } //sub class of customadapter class myviewholder{ imageview myimage; textview mytitle; textview mydescription; myviewholder(view v){ myimage= (imageview) v.findviewbyid(r.id.imageview); mytitle = (textview) v.findviewbyid(r.id.ettitle); mydescription = (textview) v.findviewbyid(r.id.etdescriptions); } } //get view method of customadapter class public view getview(int position, view convertview, viewgroup parent){ view row=convertview; myviewholder myviewholder =null; if (row==null) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); row = inflater.inflate(r.layout.row, parent, false); myviewholder = new myviewholder(row); log.d("list view","creating new row"); row.settag(myviewholder); }else { myviewholder = (myviewholder) row.gettag(); log.d("list view","recycling stuff"); } myviewholder.myimage.setimageresource(images[position]); myviewholder.mytitle.settext(titlearray[position]); myviewholder.mydescription.settext(descriptionarrayl[position]); return row; } }
mainactivity.class
package com.faisal.listviewtask; import android.content.res.resources; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.adapterview; import android.widget.listview; import android.widget.toast; public class mainactivity extends appcompatactivity { listview listview; string[] memetitles; string[] memedescriptions; customadapter customadapter; int[] image = {r.drawable.meme1, r.drawable.meme2, r.drawable.meme3, r.drawable.meme4, r.drawable.meme5, r.drawable.meme6, r.drawable.meme7, r.drawable.meme8, r.drawable.meme9, r.drawable.meme10, r.drawable.meme1, r.drawable.meme2, r.drawable.meme3, r.drawable.meme4, r.drawable.meme5, r.drawable.meme6, r.drawable.meme7, r.drawable.meme8, r.drawable.meme9, r.drawable.meme10}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (listview) findviewbyid(r.id.listview); resources res = getresources(); memetitles = res.getstringarray(r.array.titles); memedescriptions = res.getstringarray(r.array.descriptions); customadapter = new customadapter(this, memetitles, image, memedescriptions); listview.setadapter(customadapter); listview.setitemscanfocus(false); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { toast.maketext(mainactivity.this, "you clicked" + i, toast.length_long).show(); log.d("onitemclick listener","called"); } }); } }
main_activity.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:descendantfocusability="blocksdescendants" tools:context="com.faisal.listviewtask.mainactivity"> <listview android:clickable="true" android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" > </listview> </relativelayout>
row layout.xml
<?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="match_parent" > <imageview android:focusable="false" android:focusableintouchmode="false" android:id="@+id/imageview" android:layout_width="46dp" android:layout_height="46dp" android:layout_margin="10dp" android:layout_alignparenttop="true" android:layout_alignparentstart="true" /> <textview android:focusable="false" android:focusableintouchmode="false" android:id="@+id/ettitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpersonname" android:text="title" android:textstyle="bold" android:textsize="25sp" android:layout_aligntop="@+id/imageview" android:layout_torightof="@id/imageview" android:layout_alignparentright="true" /> <textview android:focusable="false" android:focusableintouchmode="false" android:id="@+id/etdescriptions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpersonname" android:text="name" android:textsize="15sp" android:layout_below="@+id/ettitle" android:layout_alignparentend="true" android:layout_toendof="@+id/imageview" /> </relativelayout>
sorry english. code given. added tags android:focusable="false"
, android:focusableintouchmode="false"
in row of list view , added android:descendantfocusability="blocksdescendants"
in root layout of listview , listview.setitemscanfocus(false);
in code before listview.setonitemclicklistener
after onitem click listener doe's not work , no response.
add in activity [updated] customadapter = new customadapter(this, memetitles, image, memedescriptions,new view.onclicklistener() { @override public void onclick(view view) { int i=(integer)view.gettag(); toast.maketext(mainactivity.this, "you clicked" + i, toast.length_long).show(); log.d("onitemclick listener","called"); } }); listview.setadapter(customadapter); , add id rootlayout in row xml public class customadapter extends arrayadapter<string> { context context; int[] images; string[] titlearray; string[] descriptionarrayl; onclicklistener onclicklistener; public customadapter(context c,string[] memetitles,int[] imgs,string[] descriptionarrayl,onclicklistener onclicklistener) { super(c,r.layout.row,r.id.ettitle,memetitles); this.context=c; this.images = imgs; this.titlearray = memetitles; this.descriptionarrayl=descriptionarrayl; this.onclicklistener=onclicklistener; } //sub class of customadapter class myviewholder{ imageview myimage; textview mytitle; textview mydescription; relativelayout rootlayout; myviewholder(view v){ rootlayout=(relativelayout)v.findviewbyid(r.id.rootlayout); myimage= (imageview) v.findviewbyid(r.id.imageview); mytitle = (textview) v.findviewbyid(r.id.ettitle); mydescription = (textview) v.findviewbyid(r.id.etdescriptions); } } //get view method of customadapter class public view getview(int position, view convertview, viewgroup parent){ view row=convertview; myviewholder myviewholder =null; if (row==null) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); row = inflater.inflate(r.layout.row, parent, false); myviewholder = new myviewholder(row); log.d("list view","creating new row"); row.settag(myviewholder); }else { myviewholder = (myviewholder) row.gettag(); log.d("list view","recycling stuff"); } myviewholder.myimage.setimageresource(images[position]); myviewholder.mytitle.settext(titlearray[position]); myviewholder.mydescription.settext(descriptionarrayl[position]); rootlayout.settag(position); rootlayout.setonclicklistener(onclicklistener); return row; } } row layout.xml <?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="match_parent" > <relativelayout android:id="@+id/rootlayout" android:layout_width="match_parent" android:layout_height="match_parent" > <imageview android:focusable="false" android:focusableintouchmode="false" android:id="@+id/imageview" android:layout_width="46dp" android:layout_height="46dp" android:layout_margin="10dp" android:layout_alignparenttop="true" android:layout_alignparentstart="true" /> <textview android:focusable="false" android:focusableintouchmode="false" android:id="@+id/ettitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpersonname" android:text="title" android:textstyle="bold" android:textsize="25sp" android:layout_aligntop="@+id/imageview" android:layout_torightof="@id/imageview" android:layout_alignparentright="true" /> <textview android:focusable="false" android:focusableintouchmode="false" android:id="@+id/etdescriptions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpersonname" android:text="name" android:textsize="15sp" android:layout_below="@+id/ettitle" android:layout_alignparentend="true" android:layout_toendof="@+id/imageview" /> </relativelayout> </relativelayout>
Comments
Post a Comment