简体   繁体   中英

How to get Selected CardView position on OnClickListener 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.

When I click on particular CardView I Toast a message but can't find position of selected cardview and want to pass details of selected contact to another activity using intent.putExtra Method.

Code :

  @Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_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());
    holder.card_view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    Toast.makeText(this.ctx, "Hello", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(this.ctx,task_detail.class);
    this.ctx.startActivity(intent);
}

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

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

    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);
        card_view =  (CardView) view.findViewById(R.id.card_view);
    }
}

Use the below code . Instead of using holder.card_view.setOnClickListener(this); inside onBindViewHolder , use it in ContactViewHolder like this , card_view.setOnClickListener(this); so that you can get the position using getPosition() method.

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

    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);
        card_view =  (CardView) view.findViewById(R.id.card_view);
        card_view.setOnClickListener(this);
    }
 @Override
        public void onClick(View v) 
       {
          Toast.makeText(this.ctx, "position "+getPosition(), Toast.LENGTH_SHORT).show();
         Intent intent = new Intent(this.ctx,task_detail.class);
          this.ctx.startActivity(intent);
       }
}

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