Android: class ContactsAdapter 中的构造函数 ContactsAdapter 无法应用于给定类型
Android: constructor ContactsAdapter in class ContactsAdapter cannot be applied to given types
我正在尝试用 ArrayList 填充 ListView
这是我的代码:
ArrayList<Contact> results = mapper.readValue(response.toString(),
new TypeReference<ArrayList<Contact>>() { } );//results are coming OK
ContactsAdapter adapter = new ContactsAdapter(this, R.layout.list_view_items, results);//error is here
这是我的自定义适配器 class,及其各自的构造函数:
public class ContactsAdapter extends ArrayAdapter<Contact> {
ArrayList<Contact> contactList = new ArrayList<>();
public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> objects) {
super(context, textViewResourceId, objects);
contactList = objects;
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_items, null);
TextView contactTextView = (TextView) v.findViewById(R.id.contactTextView);
TextView phoneTextView = (TextView) v.findViewById(R.id.phoneTextView);
ImageView imageView = (ImageView) v.findViewById(R.id.smallImageView);
contactTextView.setText(contactList.get(position).getName());
phoneTextView.setText(contactList.get(position).getPhone().toString());
//imageView.setImageResource(animalList.get(position).getAnimalImage());
return v;
}
}
错误:
Error:(58, 51) error: constructor ContactsAdapter in class
ContactsAdapter cannot be applied to given types; required:
Context,int,ArrayList found: >,int,ArrayList reason: actual argument
> cannot be converted to Context by
method invocation conversion
我的数据是从一个jSON中抓取的,声明为ArrayList。我已经尝试了一些方法,比如修改构造函数,但我没有让它工作,我被卡住了。
提前致谢!
Error:(58, 51) error: constructor ContactsAdapter in class ContactsAdapter cannot be applied to given types; required: Context,int,ArrayList<Contact> found: <anonymous Listener<JSONArray>>,int,ArrayList<Contact> reason: actual argument <anonymous Listener<JSONArray>> cannot be converted to Context by method invocation conversion
您的 Adapter
构造函数调用显然在匿名 class 中,因此 this
指的是匿名 class,而不是您需要的 Context
第一个参数。如果该代码在 Activity
中,只需将 Activity
名称添加到 this
之前;例如,MyActivity.this
.
ContactsAdapter adapter = new ContactsAdapter(MyActivity.this,
R.layout.list_view_items,
results);
我正在尝试用 ArrayList 填充 ListView
这是我的代码:
ArrayList<Contact> results = mapper.readValue(response.toString(),
new TypeReference<ArrayList<Contact>>() { } );//results are coming OK
ContactsAdapter adapter = new ContactsAdapter(this, R.layout.list_view_items, results);//error is here
这是我的自定义适配器 class,及其各自的构造函数:
public class ContactsAdapter extends ArrayAdapter<Contact> {
ArrayList<Contact> contactList = new ArrayList<>();
public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> objects) {
super(context, textViewResourceId, objects);
contactList = objects;
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_items, null);
TextView contactTextView = (TextView) v.findViewById(R.id.contactTextView);
TextView phoneTextView = (TextView) v.findViewById(R.id.phoneTextView);
ImageView imageView = (ImageView) v.findViewById(R.id.smallImageView);
contactTextView.setText(contactList.get(position).getName());
phoneTextView.setText(contactList.get(position).getPhone().toString());
//imageView.setImageResource(animalList.get(position).getAnimalImage());
return v;
}
}
错误:
Error:(58, 51) error: constructor ContactsAdapter in class ContactsAdapter cannot be applied to given types; required: Context,int,ArrayList found: >,int,ArrayList reason: actual argument > cannot be converted to Context by method invocation conversion
我的数据是从一个jSON中抓取的,声明为ArrayList。我已经尝试了一些方法,比如修改构造函数,但我没有让它工作,我被卡住了。
提前致谢!
Error:(58, 51) error: constructor ContactsAdapter in class ContactsAdapter cannot be applied to given types; required: Context,int,ArrayList<Contact> found: <anonymous Listener<JSONArray>>,int,ArrayList<Contact> reason: actual argument <anonymous Listener<JSONArray>> cannot be converted to Context by method invocation conversion
您的 Adapter
构造函数调用显然在匿名 class 中,因此 this
指的是匿名 class,而不是您需要的 Context
第一个参数。如果该代码在 Activity
中,只需将 Activity
名称添加到 this
之前;例如,MyActivity.this
.
ContactsAdapter adapter = new ContactsAdapter(MyActivity.this,
R.layout.list_view_items,
results);