Search code examples
androidlistviewandroid-arrayadapteronitemclicklistener

listview setonitemclicklistener doesn't work?


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>

This is all my code given. I added all tags like android:focusable="false", android:focusableInTouchMode="false" in row of list view and also I added android:descendantFocusability="blocksDescendants" in root layout of listview and listView.setItemsCanFocus(false); in code before the listView.setOnItemClickListener but after this onitem click listener does not work and no response.


Solution

  • if you your all code is correct and also no Exception is remain in your code But your ListView On ItemClickListener is not working then you need to use
    Restart/invalidate caches option available in Android studio then Run the Listview Project then OnItemClick Listener work correctly. Click on the File Option on top left corner of the android studio the select the invalidate caches/restart option .