Onclicklistner not working in snippet list

I have a listview with a custom adapter in listfragment, as well as set onclicklistner for listview. But Onclicklistner does not work.

Here is my code:

public class BasicFragment extends ListFragment { ListView lv; MyCustomAdapter adapter; @Override public void onCreate(Bundle si) { super.onCreate(si); } @Override public void onActivityCreated(Bundle b) { super.onActivityCreated(b); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_basic, container, false); lv = (ListView) view.findViewById(android.R.id.list); FetchedData DT = FetchedData.StaticDataTransfer(); RecepiesProperties[] AryObjaz = DT.getData(); getdata(AryObjaz); adapter = new MyCustomAdapter(getActivity(), R.layout.listview_layout, Dataset); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast t = Toast.makeText(getActivity(), "Message", Toast.LENGTH_SHORT); t.show(); } }); return view; }} 

MyCustomAdapter.java

 public class MyCustomAdapter extends ArrayAdapter<Recipes> { Context context; int layoutResourceId; Recipes data[] = null; Typeface typeface; public ImageLoader imageLoader; public MyCustomAdapter(Context context, int textViewResourceId, Recipes[] dataset) { super(context, textViewResourceId, dataset); this.layoutResourceId = textViewResourceId; this.context = context; this.data = dataset; imageLoader = new ImageLoader(context.getApplicationContext()); } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); RecipesHolder holder = new RecipesHolder(); holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1); holder.txtTitle = (TextView) row.findViewById(R.id.title); holder.category = (TextView) row.findViewById(R.id.category); holder.source = (TextView) row.findViewById(R.id.source); holder.country = (TextView) row.findViewById(R.id.country); holder.readytime = (TextView) row.findViewById(R.id.readytime); holder.tips = (Button) row.findViewById(R.id.tips); holder.fav = (Button) row.findViewById(R.id.fav); Recipes ap = data[position]; imageLoader.DisplayImage(ap.getIMAGENAME240(), holder.imgIcon); holder.txtTitle.setText(ap.getNAME()); holder.category.setText(ap.getCATEGORY()); holder.source.setText(ap.getSOURCE()); holder.country.setText(ap.getCOUNTRY()); holder.readytime.setText(ap.getREADYTIME()); return row; } static class RecipesHolder { ImageView imgIcon; TextView txtTitle; TextView category; TextView source; TextView country; TextView readytime; Button tips; Button fav; }} 

//listview_layout.xml

 <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"><ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignParentLeft="true" android:focusable="true" android:layout_alignParentTop="true" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" /> <TextView android:id="@+id/readytime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/country" android:layout_alignBottom="@+id/country" android:layout_marginLeft="73dp" android:layout_toRightOf="@+id/country" android:focusable="true" android:text="TextView" android:textColor="#000" /> <TextView android:id="@+id/country" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView1" android:layout_alignLeft="@+id/source" android:focusable="true" android:text="TextView" android:textColor="#000" /> <TextView android:id="@+id/source" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/country" android:layout_alignLeft="@+id/category" android:focusable="true" android:text="TextView" android:textColor="#000" /> <TextView android:id="@+id/category" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/source" android:layout_alignLeft="@+id/title" android:text="TextView" android:focusable="true" android:textColor="#000" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/category" android:layout_toRightOf="@+id/imageView1" android:text="TextView" android:focusable="true" android:textColor="#000" /> <Button android:id="@+id/fav" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tips" android:layout_below="@+id/source" android:focusable="true" android:background="@drawable/favourite" /> <Button android:id="@+id/tips" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView1" android:layout_marginRight="14dp" android:background="@drawable/yellow" /></RelativeLayout> 
+4
source share
10 answers

finally solved the problem when all the controls (buttons, text fields) in the listview are set to focal false.

+11
source

Check your MyCustomAdapter, some widgets (such as: Button, ImageButton) in the custom layout will use the click event, and then onItemClick will never be called.

Use the following code in the getView getView method to get the onClick event.

  row.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); 
+7
source

According to the Android API doc:

ListFragment has a default layout, which consists of one kind of list. However, if you want, you can configure the layout of the fragment to return your own view hierarchy from onCreateView (LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView with the identifier "@android: id / list" (or a list if it is code)

Since you have not published the layout file for the snippet, I'm not sure what is wrong here. The following code is how it should be when you use the default list view for ListFragment. If you are using ListFragment, you must use additional methods available as setListAdapter and onListItemClick . You can also do the same without using a ListFragment (using just a fragment).

Fragment code (code fragment changed)

 public class BasicFragment extends ListFragment { MyCustomAdapter adapter; @Override public void onCreate(Bundle si) { super.onCreate(si); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // not sure what you are doing here but data fetch should be asynchronous if it interacts with DB or makes network call FetchedData DT = FetchedData.StaticDataTransfer(); RecepiesProperties[] AryObjaz = DT.getData(); getdata(AryObjaz); adapter = new MyCustomAdapter(getActivity(), R.layout.listview_layout, Dataset); setListAdapter(adapter); } @Override public void onListItemClick(ListView l, View v, int position, long id) { Toast t = Toast.makeText(getActivity(), "Message", Toast.LENGTH_SHORT); t.show(); } } 

Also, I changed the adapter code to help rework the views, your current code did not use view rework and always inflated the views.

Adapter Code:

 public class MyCustomAdapter extends ArrayAdapter<Recipes> { Context context; int layoutResourceId; Recipes data[] = null; Typeface typeface; public ImageLoader imageLoader; private LayoutInflater inflater; public MyCustomAdapter(Context context, int textViewResourceId, Recipes[] dataset) { super(context, textViewResourceId, dataset); this.layoutResourceId = textViewResourceId; this.context = context; this.data = dataset; imageLoader = new ImageLoader(context.getApplicationContext()); inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; RecipesHolder holder = null; //recycling views if(null == row){ row = inflater.inflate(layoutResourceId, parent, false); holder = new RecipesHolder(); holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1); holder.txtTitle = (TextView) row.findViewById(R.id.title); holder.category = (TextView) row.findViewById(R.id.category); holder.source = (TextView) row.findViewById(R.id.source); holder.country = (TextView) row.findViewById(R.id.country); holder.readytime = (TextView) row.findViewById(R.id.readytime); holder.tips = (Button) row.findViewById(R.id.tips); holder.fav = (Button) row.findViewById(R.id.fav); row.setTag(holder); }else{ holder = (RecipesHolder)row.getTag(); } Recipes ap = data[position]; imageLoader.DisplayImage(ap.getIMAGENAME240(), holder.imgIcon); holder.txtTitle.setText(ap.getNAME()); holder.category.setText(ap.getCATEGORY()); holder.source.setText(ap.getSOURCE()); holder.country.setText(ap.getCOUNTRY()); holder.readytime.setText(ap.getREADYTIME()); return row; } static class RecipesHolder { ImageView imgIcon; TextView txtTitle; TextView category; TextView source; TextView country; TextView readytime; Button tips; Button fav; } } 
+6
source

Put the following code in the onViewCreated() method:

 @Override public void onViewCreated(View view, Bundle savedInstanceState) { ListView list = (ListView) view.findViewById(android.R.id.list); list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast t = Toast.makeText(getActivity(), "Message", Toast.LENGTH_SHORT); t.show(); } }); } 

The problem may be that the view is not yet constructed in onViewCreate (), so it is not recommended to set listeners there.

+3
source

When you inflate your views, it seems that there are 2 buttons in your view:

this row.findViewById(R.id.tips);

and this row.findViewById(R.id.fav);

You must either remove these buttons (replace them with text images if you do not want to click them)

Or, if you want clicks of these buttons, you can use OnClickListeners for each button and another OnClickListener for the entire view.


Example

 @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; //we only inflate if the row is null!! if(row == null) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); RecipesHolder holder = new RecipesHolder(); holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1); holder.txtTitle = (TextView) row.findViewById(R.id.title); holder.category = (TextView) row.findViewById(R.id.category); holder.source = (TextView) row.findViewById(R.id.source); holder.country = (TextView) row.findViewById(R.id.country); holder.readytime = (TextView) row.findViewById(R.id.readytime); holder.tips = (Button) row.findViewById(R.id.tips); holder.fav = (Button) row.findViewById(R.id.fav); //we set the tag of the view to this holder so we can get it everytime row.setTag(holder); } //change this to final so that you can use it inside your click listeners final Recipes ap = data[position]; //here we get the holder of the view from its tag RecipesHolder holder = (RecipesHolder) row.getTag(); //no changes to ur setup imageLoader.DisplayImage(ap.getIMAGENAME240(), holder.imgIcon); holder.txtTitle.setText(ap.getNAME()); holder.category.setText(ap.getCATEGORY()); holder.source.setText(ap.getSOURCE()); holder.country.setText(ap.getCOUNTRY()); holder.readytime.setText(ap.getREADYTIME()); //now the click listeners //tips button holder.tips.setOnClickListener(new OnClickListener() { public void onClick(View v) { //tips has been clicked //do whatever you want with `ap` } }); //fav button holder.fav.setOnClickListener(new OnClickListener() { public void onClick(View v) { //fav has been clicked //do whatever you want with `ap` } }); //whole item click row.setOnClickListener(new OnClickListener() { public void onClick(View v) { //the row has been clicked //do whatever you want with `ap` } }); return row; } 
+3
source
  lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //your code } }); 

I think this should work

+2
source

The adapter has a method areAllItemsEnabled (), it can be useful.

public abstract boolean areAllItemsEnabled ()

Indicates whether all elements of this adapter are enabled. If the value returned by this method changes over time, there is no guarantee that it will take effect. If true, this means that all items can be selected and clicked (no separator.)

+2
source
 @Override public void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); } 
+1
source

Just put android:focusable="false" android:clickable="false" in the layout. For all text views, buttons, etc. The problem is resolved.

0
source

I had a similar problem, it took me a lot of time to understand what happened. Earlier, my onListItemClick () fragment opened a new activity, but after returning to the fragment, the onListItemClick () listener did not work anymore.

 @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); // additional code (eg open new activity) } 

This problem was resolved by creating a listener inside my custom ArrayAdapter. I placed this code inside getView ():

  rowView.setOnClickListener(new View.OnClickListener() { final int p = position; @Override public void onClick(View v) { Log.d("FeedListAdapter", "onClick()"); openPost(p); // for example } }); 

I did not sleep all night, fixing it (it's 8am), I thought I should publish my decision for everyone who is in a similar place :)

0
source

All Articles