gpt4 book ai didi

Android:类 ContactsAdapter 中的构造函数 ContactsAdapter 不能应用于给定类型

转载 作者:行者123 更新时间:2023-11-29 19:40:37 25 4
gpt4 key购买 nike

我正在尝试用 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

这是我的自定义适配器类,及其各自的构造函数:

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 构造函数调用显然是在一个匿名类中,所以 this 指的是那个匿名类,而不是你需要的 Context第一个参数。如果该代码在 Activity 中,只需将 Activity 名称添加到 this 之前;例如,MyActivity.this

ContactsAdapter adapter = new ContactsAdapter(MyActivity.this,
R.layout.list_view_items,
results);

关于Android:类 ContactsAdapter 中的构造函数 ContactsAdapter 不能应用于给定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38680149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com