如何将List <e>从RecyclerView适配器传递到RecyclerView Holder?

时间:2017-12-06 16:20:22

标签: java android android-recyclerview recycler-adapter

我一直试图找到上述问题的答案,但没有任何帖子似乎有帮助。我有一个RecyclerView适配器,我从数据库中获取List作为List:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {

    public List<Note> noteList;
    private View.OnLongClickListener longClickListener;
    private Context context;

    public RecyclerViewAdapter(List<Note> noteList, View.OnLongClickListener longClickListener, Context context) {
        this.noteList = noteList;
        this.longClickListener = longClickListener;
        this.context = context;
    }

还有一个ViewHolder,我有一个onClickListener来查找使用getLayoutPosition()点击的视图的位置:

static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView noteTitleText;
        private TextView noteDescriptionText;
        private TextView dateText;
        private TextView weekText;

        RecyclerViewHolder(View view) {
            super(view);

            noteTitleText = (TextView) view.findViewById(R.id.noteTitleText);
            noteDescriptionText = (TextView) view.findViewById(R.id.noteDescriptionText);
            dateText = (TextView) view.findViewById(R.id.dateText);
            weekText = (TextView) view.findViewById(R.id.weekText);

            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            int position = getLayoutPosition();

            // I want the noteList from the RecyclerViewAdapter here.
            Note note = noteList.get(position); 

            Log.d("Item Clicked", String.valueOf(position));

        }
    }

目的是我需要noteList以及onClick()方法中RecyclerViewAdapter的所有注释,以使用intent填充Activity。我应该怎么做呢?

1 个答案:

答案 0 :(得分:0)

在尝试了一小时的解决方案之后,这就是我所确定的。我没有在ViewHolder中实现OnClickListener,而是在onBindViewHolder()方法的RecyclerViewAdapter中实现了它,如下所示:

int dim=0;//<-- Here!!
char common[dim];