I have a RecyclerView
that utilizes a Recycler Adapter to output a list layout, like this:
http://i.imgur.com/ORkXXTb.png
I need to attach the model below to each of the list items, such that if the user clicks on any element in the list item (like the circle or one of the two TextViews), it passes the model object to the next Activity
.
Here is the User
model:
public class User {
private String id;
private String username;
private String displayName;
private Object deletedAt;
private Statistic stat;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username= username;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public Object getDeletedAt() {
return deletedAt;
}
public void setDeletedAt(Object deletedAt) {
this.deletedAt = deletedAt;
}
public Statistic getStat() {
return stat;
}
public void setStat(Statistic stat) {
this.stat = stat;
}
}
Here is the layout for each list item (user_layout.xml
):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/user_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white">
<ImageView
android:id="@+id/avatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="@drawable/avatar" />
<TextView
android:id="@+id/display_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/avatar"
android:layout_toRightOf="@+id/avatar"
/>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/display_name"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Here is the UserRecyclerAdapter
that's used to inflate the layout above:
public class UserRecyclerAdapter extends RecyclerView.Adapter<UserRecyclerAdapter.ViewHolder> {
private Context context;
private List<User> mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView avatar;
public TextView displayName;
public TextView username;
public ViewHolder(LinearLayout view) {
super(view);
avatar = (ImageView) view.findViewById(R.id.avatar);
displayName = (TextView) view.findViewById(R.id.display_name);
username = (TextView) view.findViewById(R.id.username);
}
}
public UserRecyclerAdapter(Context context, List<User> myDataset) {
this.context = context;
this.mDataset = myDataset;
}
@Override
public UserRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_layout, parent, false);
ViewHolder vh = new ViewHolder((LinearLayout) view);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
User userItem = mDataset.get(position);
holder.displayName.setText(userItem.getDisplayName());
holder.username.setText(userItem.getUsername());
}
@Override
public int getItemCount() {
return mDataset.size();
}
}
So my question is, how can I attach the User
model object to each list item so that when an element (like the circle or two TextViews) are clicked, it passes the model object to the next Activity
?
Thanks.