Reputation: 571
i'm trying to populate a ListView with a ArrayList
this is my code:
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
and this is my custom adapter class, with its respective constructor:
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:
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
My data is grabbed from a jSON, and it is declared as ArrayList. I've tried some things like modifying the constructor, but i don't get it to work and i'm stuck.
Thanks in advance!
Upvotes: 1
Views: 2165
Reputation: 39181
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
Your Adapter
constructor call is apparently inside an anonymous class, so this
refers to that anonymous class, not the Context
you need as the first argument. If that code is in an Activity
, just prepend the Activity
name to this
; e.g., MyActivity.this
.
ContactsAdapter adapter = new ContactsAdapter(MyActivity.this,
R.layout.list_view_items,
results);
Upvotes: 2