我需要将点击按钮添加到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();
}
});
有人可以帮帮我吗?
致谢
答案 0 :(得分:0)
替换&#34; 此&#34;行
中的上下文 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 refrence
注意:您可以在VideoAdapter构造函数中传递尽可能多的参数,这只是向您展示如何传递上下文的方法。