[英]error: incompatible types: Context cannot be converted to ArrayList<dataModel>
I don't know what am I supposed to do, can somebody help me??我不知道我该怎么办,有人可以帮助我吗?
error: incompatible types: Context cannot be converted to ArrayList adapter = new myAdapter(getApplicationContext());
错误:不兼容的类型:上下文无法转换为 ArrayList 适配器 = new myAdapter(getApplicationContext());
can you help me to solve my problem??你能帮我解决我的问题吗?
this is my MainActivity.java:这是我的 MainActivity.java:
adapter = new myAdapter(getApplicationContext());
recycler.setAdapter(adapter);
here is myAdapter.java:这是 myAdapter.java:
public class myAdapter extends RecyclerView.Adapter<myAdapter.myHolder> {
ArrayList<dataModel> dataHolder;
Context context;
public myAdapter(ArrayList<dataModel> dataHolder) {
this.dataHolder = dataHolder;
//this.context = context;
}
}
So, your getApplicationContext()
method returns Context
, but in your constructor you expect parameter of type ArrayList<dataModel>
, that's why it throws exception.因此,您的
getApplicationContext()
方法返回Context
,但在您的构造函数中,您期望ArrayList<dataModel>
类型的参数,这就是它抛出异常的原因。 Now you have 2 options:现在您有 2 个选项:
keep your constructor, but pass different parameter保留您的构造函数,但传递不同的参数
adapter = new myAdapter(someArrayList);
change constructor, so that it accepts Context
更改构造函数,使其接受
Context
public myAdapter(Context context) { this.dataHolder = new ArrayList<>(); this.context = context; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.