Search code examples
androidlistviewcustom-adapteronitemclicklistenerandroid-viewholder

How to make a setOnItemClickListener work for a ListView with a CustomAdapter?


I have a little project to manage the landed tricks for football freestylers.

The app is like that TVShowTime app that manages our shows.

The landed button is working, inside the item list, see my list view below.

listview

Everything works fine but when I click in any item from the list nothing happens.

Thank you.

CustomAdapter

public class CustomAdapter extends ArrayAdapter  {

    private String[] tricks;
    private ImageView imageView;
    private Activity context;
    private boolean isClicked;

    public CustomAdapter(@NonNull Activity context, String[] tricks, ImageView unmastered) {
        super(context, R.layout.custom_listview_tricks, tricks);

        this.context = context;
        this.tricks = tricks;
        this.imageView = unmastered;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View r= convertView;

        viewHolder viewHolder = null;

        if(r==null){

            LayoutInflater layoutInflater = context.getLayoutInflater();
            r = layoutInflater.inflate(R.layout.custom_listview_tricks, null, true);
            viewHolder = new viewHolder(r);
            r.setTag(viewHolder);
        } else {

            viewHolder = (CustomAdapter.viewHolder) r.getTag();

        }

        viewHolder.textView1.setText(tricks[position]);

        viewHolder.unmastered.setBackgroundResource(R.drawable.unmastered);

        final CustomAdapter.viewHolder finalViewHolder = viewHolder;

        viewHolder.unmastered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if(isClicked){
                    finalViewHolder.unmastered.setBackgroundResource(R.drawable.mastered);
                }else{
                    finalViewHolder.unmastered.setBackgroundResource(R.drawable.unmastered);
                }
                isClicked = !isClicked;

            }
        });

viewHolder.textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(getContext(), "TEST", Toast.LENGTH_SHORT).show();

            }
        });

        return r;
    }


    class viewHolder{

        TextView textView1;
        ImageView unmastered;
        viewHolder (View v){

            textView1 = v.findViewById(R.id.name_xml);
            unmastered = v.findViewById(R.id.unmastered_xml);

        }

    }

}

TricksActivity

public class TricksActivity extends AppCompatActivity {

    private String[] lower = {

            "ATW - Around the World",
            "HTW - Hop the World",
            "Crossover",
            "Crossover 360",
            "Simple Crossover",
            "Reverse Crossover",
            "KATW - Knee Around the World",
            "KHTW - Knee Hop the World",
            "Toe Bounce",
            "Reverse Toe Bounce",
            "Air Jester",
            "ATL - Around the Leg",
            "Hell Juggles"

    };

    private ImageView imageView;


    private int codigo;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tricks);


        imageView = findViewById(R.id.unmastered_xml);
        //imageView.setBackgroundResource(R.drawable.mastered);


        Intent intent = getIntent();
        codigo = intent.getIntExtra("codigo", 0);

        //Toast.makeText(this, ""+codigo, Toast.LENGTH_SHORT).show();

        listView = findViewById(R.id.listview_tricks_xml);

        CustomAdapter customAdapter = new CustomAdapter(this, lower, imageView);

        listView.setAdapter(customAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

             Toast.makeText(TricksActivity.this, ""+position, Toast.LENGTH_SHORT).show();

            }

            }
        });

    }

}

activity_tricks.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dlapps.tricksmasterizadas.tricksmasterizadas.TricksActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="15dp">


        <ListView
            android:id="@+id/listview_tricks_xml"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:dividerHeight="1dp"
            >

        </ListView>

    </LinearLayout>




</RelativeLayout>

custom_listview_tricks.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="310dp"
        android:layout_height="50dp"
        android:text="1  ATW - Around the World"
        android:id="@+id/name_xml"
        android:textStyle="bold"
        android:textSize="20dp"
        android:gravity="center"
        android:layout_marginTop="3dp"/>

    <ImageButton
        android:id="@+id/unmastered_xml"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="315dp"
        android:layout_marginTop="3dp"
        android:background="@drawable/unmastered" />

</RelativeLayout>

Solution

  • Problem solved

    I just added this code inside the custom_list_view.xml and finally setOnClickListener worked.

    android:descendantFocusability="blocksDescendants"
    

    Now my layout is:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:descendantFocusability="blocksDescendants"
        android:layout_width="match_parent"
        android:layout_height="match_parent">