在RecyclerView中获取选定的TextView值

时间:2016-02-06 07:26:32

标签: android android-studio android-recyclerview

我正在尝试从TextView中的所选项中获取RecyclerView值。我的RecyclerView布局文件有两个TextView个,一个用于名称,另一个用于日期,如您所见:

<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" />

这是我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);
    }
}

这是onClick事件,它启动了我之前提到的其他活动:

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);
                        }
                    })
    );

这是为ListRecyclerView设置值的类:

public String name;
public String date;

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

如何从RecyclerView中的所选项目中获取名称字段并将其传递给其他活动?有什么建议吗?

1 个答案:

答案 0 :(得分:2)

您可以使用viewHolder.name.getText(list.get(position).name)。这将从TextView处获取存储在变量position中的位置的文本。