简体   繁体   中英

Clickable items form a ListView : setOnItemClickListener won't work

I'm currently working on an android project and I need to launch an activity from an item of a ListView. I searched more or less 8 hours and found always the same solution, which is not working. Here is my code :

In the onCreate method of CatalogueActivity :

CtlgArrayAdapter adapter = new CtlgArrayAdapter(this, recipeName, recipeDescr, recipeDiff,
            recipePic, recipeEval);
    ListView list = (ListView)findViewById(android.R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "Item clicked");
        }
    });    

The CtlgArrayAdapter class :

public class CtlgArrayAdapter extends ArrayAdapter<String> {
private static final String TAG = "CTLGADAPTER";

private final Activity context;
private final String[] recipeName;
private final String[] recipeDescr;
private final Integer[] recipeDiff;
private final Integer[] recipePic;
private final Integer[] recipeEval;

public CtlgArrayAdapter(Activity context, String[] recipeName, String[] recipeDescr,
                        Integer[] recipeDiff, Integer[] recipePic, Integer[] recipeEval) {
    super(context, R.layout.ctlg_list, recipeName);

    this.context = context;
    this.recipeName = recipeName;
    this.recipeDescr = recipeDescr;
    this.recipeDiff = recipeDiff;
    this.recipePic = recipePic;
    this.recipeEval = recipeEval;
}

public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.ctlg_list, null, true);

    TextView nameTile = (TextView) rowView.findViewById(R.id.ctlg_list_name);
    TextView diffTile = (TextView) rowView.findViewById(R.id.ctlg_list_diff);
    TextView descrTile = (TextView) rowView.findViewById(R.id.ctlg_list_descr);
    ImageView picTile = (ImageView) rowView.findViewById(R.id.ctlg_list_img);
    RatingBar rateTile = (RatingBar) rowView.findViewById(R.id.ctlg_list_rating);
    nameTile.setText(recipeName[position]);
    diffTile.setText(recipeDiff[position].toString());
    descrTile.setText(recipeDescr[position]);
    picTile.setImageResource(recipePic[position]);
    rateTile.setRating(recipeEval[position]);

    return rowView;
}

The problem is that when I tap my item, nothing happens. All the internet seems to say that this the solution, but it doesn't work :'(

Here are my .xml files : catalogue layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
    android:id="@android:id/list"
    android:clickable="true"
    android:layout_alignParentStart="true"
    android:layout_below="@id/linear_ctlg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/> </RelativeLayout>    

and the catalogue_item layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/ctlg_list_img"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:focusable="false"
    android:scaleType="centerCrop"
    android:src="@drawable/no_image"
    android:layout_marginEnd="10dp"/>
<TextView
    android:id="@+id/ctlg_list_name"
    android:textColor="@color/colorPrimary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@id/ctlg_list_img"
    android:focusable="false"
    android:textStyle="bold"
    android:text="Nom de la recette sur son lit de char"/>
<TextView
    android:id="@+id/ctlg_list_diff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@id/ctlg_list_img"
    android:layout_below="@id/ctlg_list_name"
    android:text="Difficile"/>
<TextView
    android:id="@+id/ctlg_list_descr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ctlg_list_diff"
    android:layout_toEndOf="@id/ctlg_list_img"
    android:focusable="false"
    android:textStyle="italic"
    android:text="Ceci est la description de la recette. J'en mets une longue pour être sûr qu'il y ait assez d'espace. Il fait beau aujourd'hui."/>
<RatingBar
    android:id="@+id/ctlg_list_rating"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:focusable="false"
    android:scaleX="0.4"
    android:scaleY="0.4"
    android:numStars="5"
    android:rating="3"
    android:stepSize="1"/> </RelativeLayout>

Thank you sooooooo much if you find what's wrong with my code ! :)

Make sure none of your views in catelogue_item are focusable or clickable, use the code below.

android:clickable="false" 
android:focusable="false"
android:focusableInTouchMode="false"

Also try with setting the root layout with: android:descendantFocusability="blocksDescendants"

The reason is if any of your element in the list contains focusable item , the onclick of list will not be called.

I'm not sure whether its right solution.

Your passing activity instead of context here

super(context, R.layout.ctlg_list, recipeName); , so try to pass context.

Check this post which is similar to code it might be useful.

You dont need any focusable:false or clickable:false . you can remove all of them. just add this code android:descendantFocusability="blocksDescendants" in your catalogue xml top relative layout . I tried it with your code and worked.

I found a solution : I suppressed the rating bar from the catalogue_tiem layout and now everything works perfectly (I don't need to keep it in this layout because it will be displayed later). I suppose that there are solutions to make it work with the rating bar, by modifying attributes in the xml file, but... nevermind :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM