Reputation: 1034
I'm using RecyclerView to bind a list of object in an adapter.
With SetOnItemClickListener I'm able to intercept clicks on elements of the RecycleView, but I need to access to the properties of the binded object (e.g: the field name) to send to other activity when is clicked on the button.
How would this be possible ?
Upvotes: 0
Views: 2807
Reputation: 164
package com.example.gmehdibalti.jasonpracticwthree;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by G Mehdi Balti on 5/16/2016.
*/
public class myAdapter extends RecyclerView.Adapter<myAdapter.ContactViewHolder>{
ArrayList<UserModel> list;
Activity activity;
public myAdapter(ArrayList<UserModel> list, Activity activity) {
this.list = list;
this.activity = activity;
}
@Override
public myAdapter.ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell, parent, false);
ContactViewHolder cvh=new ContactViewHolder(v);
return cvh;
}
@Override
public void onBindViewHolder(myAdapter.ContactViewHolder holder, int position) {
ContactViewHolder cvh=holder;
UserModel abc=list.get(position);
cvh.txt_id.setText(abc.getId());
cvh.txt_name.setText(abc.getName());
cvh.txt_email.setText(abc.getEmail());
}
@Override
public int getItemCount() {
return list.size();
}
public class ContactViewHolder extends RecyclerView.ViewHolder {
TextView txt_name;
TextView txt_id;
TextView txt_email;
public ContactViewHolder(View itemView) {
super(itemView);
txt_id = (TextView)itemView.findViewById(R.id.txt_Id_item);
txt_name = (TextView)itemView.findViewById(R.id.txt_name_item);
txt_email = (TextView)itemView.findViewById(R.id.txt_email_item);
}
}
}
Upvotes: 0
Reputation: 361
I think you are mixing the idea of ListView and RecylerView. RecylerView is ViewHolder oriented, so you should handle click event on ViewHolder itself. It's very simple.
public class ObjectClass {
public String property1;
public String property2;
...
}
public class ObjectAdapter extends RecyclerView.Adapter<ObjectViewHolder> {
private Context mContext;
private List<ObjectClass> mObjects;
public ObjectAdapter(Context context) {
mContext = context;
}
public ObjectClass getItem(int position) {
return mObjects != null ? mObjects.get(position) : null;
}
@Override
public int getItemCount() {
return mObjects != null ? mObjects.size() : 0;
}
@Override
public ObjectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ObjectViewHolder(LayoutInflater.from(mContext)
.inflate(R.layout.item_layout, parent, false));
}
@Override
public void onBindViewHolder(ObjectViewHolder holder, int position) {
// bind the item with ViewHolder here
holder.item = getItem(position);
}
public static class ObjectViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
private ObjectClass item;
public ObjectViewHolder(View view) {
super(view);
view.setOnClickListener(this);
}
public void onClick(View v) {
// do stuff with item's properties...
}
}
}
Upvotes: 3