【问题标题】:how add comments into listview in recycler view.Adapter如何在回收站 view.Adapter 的列表视图中添加评论
【发布时间】:2018-01-17 11:04:36
【问题描述】:

我需要将点击按钮上的 cmets 放入 listview,但我正在使用回收器视图适配器。

我得到这个错误:

Error:(200, 54) error: no suitable constructor found for ArrayAdapter(<anonymous OnClickListener>,int,ArrayList<String>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Context)

这是代码:

holder.button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            //holder.lw.setA
           String commento= (holder.tw.getText().toString());
           // holder.lw.setAdapter(adapter);
             ArrayList<String> arrayList= new ArrayList<String>();
            final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);

            arrayList.add(commento);
            // next thing you have to do is check if your adapter has changed
            holder.lw.setAdapter(adapter);
            adapter.notifyDataSetChanged();


        }

    });

有人可以帮助我吗? 谢谢

【问题讨论】:

  • this 替换为contextfinal ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, arrayList); 行中
  • @Chithra 没有上下文...

标签: java android listview android-recyclerview


【解决方案1】:

在该行中将“this”替换为Context

 final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);

在您的 VideoAdapter 中传递上下文

 VideoAdapter videoAdapter = new VideoAdapter (this);

VideoAdapter 的构造函数

  public VideoAdapter (Context context) {
       this.mContext = context;
    }

像这样初始化

 final ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext , android.R.layout.simple_list_item_1, arrayList);

如您所见,ArrayAdapter 的构造函数需要 Context 而不是 onClickListener 引用

注意:您可以在 VideoAdapter 构造函数中传递尽可能多的参数,这只是向您展示如何传递上下文的一种方式。

【讨论】:

  • 我留在 VideoAdapter extends RecyclerView.Adapter
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多