Razor Razor - 10 months ago 59
Android Question

Get the selected TextView value in a RecyclerView

I am trying to get a

TextView
value out of the selected item within the
RecyclerView
. My
RecyclerView
layout file has two
TextView
s, one for the name and the other for the date as you can see here:

<TextView
android:id="@+id/layout_recyclerview_view_list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/layout_recyclerview_view_list_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/layout_recyclerview_view_list_name"
android:layout_alignLeft="@+id/layout_recyclerview_view_list_name"
android:layout_below="@+id/layout_recyclerview_view_list_name" />


This is my adapter class for the
RecyclerView
:

List<ListsRecyclerViewList> list;

public ListsRecyclerViewListAdapter(List<ListsRecyclerViewList> list) {
this.list = list;
}

@Override
public ListsRecyclerViewListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recyclerview_view_list, parent, false);
ListsRecyclerViewListViewHolder viewHolder = new ListsRecyclerViewListViewHolder(view);

return viewHolder;
}

@Override
public void onBindViewHolder(ListsRecyclerViewListViewHolder holder, int position) {
holder.name.setText(list.get(position).getName());
holder.date.setText(list.get(position).getDate());
}

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

public static class ListsRecyclerViewListViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView date;

public ListsRecyclerViewListViewHolder(View view) {
super(view);

this.name = (TextView) view.findViewById(R.id.layout_recyclerview_view_list_name);
this.date = (TextView) view.findViewById(R.id.layout_recyclerview_view_list_date);
}
}


This is the
onClick
event that launches the other activity as I mentioned earlier:

recyclerView.addOnItemTouchListener(
new ListsRecyclerViewListListener(this,
new ListsRecyclerViewListListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(getBaseContext(), ListsEditListActivity.class);
Bundle bundle = new Bundle();

bundle.putString("name", /*this is where I need to set the value*/);
intent.putExtras(bundle);

startActivity(intent);
}
})
);


This is the class that sets the values for the
List
and
RecyclerView
:

public String name;
public String date;

public ListsRecyclerViewList(String name, String date) {
this.name = name;
this.date = date;
}


How can I get the name field from the selected item within the
RecyclerView
and pass it to another activity? Any suggestions?

Answer Source

You can use viewHolder.name.getText(list.get(position).name). This will get the text from a TextView at the position stored in the variable position.