Manish Tiwari Manish Tiwari - 1 month ago 19
Android Question

OnClickListener for CardView is not working android?

I have list of contacts and i have show this list on RecyclerView using CardView.

List is showing perfectly But now I want to make clickable Card View. So that When I click on particular contact i will show details of particular contact.

List is perfectly shown but when i click on particular contact click event is not working .

I have also used clickable true on CardView and OnClickListener event on ContactAdapter class .

What I did :

I have three layout activity_main, contact_list, contact_detail.

In activity_main i used recycler_view.
In contact_list i used card_view and bind it with recycler view.

I have three java class :
Contact - where i defined variable with getter and setter
ContactAdapter - where i get contacts and bind it with card view.
MainActivity - where i get all contacts and show it on recycler view.

activity_main layout code:

<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/recycler_view">

</android.support.v7.widget.RecyclerView>


contact_list_layout code:

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/card_view"
android:clickable="true">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remo"
android:id="@+id/person_name"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/person_name"
android:text="example@gmail.com"
android:id="@+id/person_email"/>

</RelativeLayout>

</android.support.v7.widget.CardView>


Java Class Code:

MainActivity Code :

RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
String[] name,email;
ArrayList<Contact> list = new ArrayList<Contact>();

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

name = getResources().getStringArray(R.array.person_name);
email = getResources().getStringArray(R.array.person_email);

int count = 0;
for (String Name : name)
{
Contact contact = new Contact(Name,email[count]);
count++;
list.add(contact);
}
recyclerView= (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter=new ContactAdapter(list,this);
recyclerView.setAdapter(adapter);
}


ContactAdapter code :

ArrayList<Contact> contacts = new ArrayList<Contact>();
Context ctx;

public ContactAdapter(ArrayList<Contact> contacts, Context ctx) {
this.contacts = contacts;
this.ctx = ctx;
}

@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_list_layout, parent, false);
ContactViewHolder contactViewHolder = new ContactViewHolder(view, ctx, contacts);
return contactViewHolder;
}

@Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
Contact CON = contacts.get(position);
holder.person_name.setText(CON.getName());
holder.person_email.setText(CON.getEmail());
}

@Override
public int getItemCount() {
return contacts.size();
}

public static class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView person_name, person_email;
ArrayList<Contact> contacts = new ArrayList<Contact>();
Context ctx;

public ContactViewHolder(View view, Context ctx, ArrayList<Contact> contacts) {
super(view);
this.contacts = contacts;
this.ctx = ctx;
person_name = (TextView) view.findViewById(R.id.person_name);
person_email = (TextView) view.findViewById(R.id.person_email);
}

@Override
public void onClick(View v) {
int position = getAdapterPosition();
Contact contact = this.contacts.get(position);
Intent intent =new Intent(this.ctx,ContactDetail.class);
this.ctx.startActivity(intent);
}
}

Answer

From your code, you are missed setting listener for your cardview. to trigger onClick(View v) method you have to set listener for the corresponding view. example:

@Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
    Contact CON = contacts.get(position);
    holder.person_name.setText(CON.getName());
    holder.person_email.setText(CON.getEmail());
    holder.card_view.setOnClickListener(this)
}

UPDATED ViewHolder

 public static class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView person_name, person_email;
        CardView card_view;
        ArrayList<Contact> contacts = new ArrayList<Contact>();
        Context ctx;

        public ContactViewHolder(View view, Context ctx, ArrayList<Contact> contacts) {
            super(view);
            this.contacts = contacts;
            this.ctx = ctx;
            card_view = (CardView) view.findViewById(R.id.card_view);
            person_name = (TextView) view.findViewById(R.id.person_name);
            person_email = (TextView) view.findViewById(R.id.person_email);
        }

EDIT

Since your parent layout is CardView you can directly set listener without initiating

Example

public ContactViewHolder(View view, Context ctx, ArrayList<Contact> contacts) {
    super(view);
    view.setOnClickListener(this);
    this.contacts = contacts;
    this.ctx = ctx;
    person_name = (TextView) view.findViewById(R.id.person_name);
    person_email = (TextView) view.findViewById(R.id.person_email);
}