Question

I am a little helpless at the moment.

I have following code

public class ListViewAdapter<String> extends ArrayAdapter<String> {
private final Context context;
private final int textViewResourceId;
private final int[] itemResourceIds;
private final ArrayList<String> list;

public ListViewAdapter(Context context, int textViewResourceId,
    int[] itemResourceIds, ArrayList<String> list) {
        super(context, textViewResourceId, itemResourceIds, list);
        this.context = context;
        this.textViewResourceId = textViewResourceId;
        this.itemResourceIds = itemResourceIds;
        this.list = list;
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // some code...
}
}

But eclipse marks the super(...) line, and I can't figure out why.

It tells me:

The constructor ArrayAdapter<String>(Context, int, int[], ArrayList<String>) is undefined

But didn't I define it exactly this way?

Please help me out here.

Was it helpful?

Solution

when you use super, you have to call one of the parent class defined constructors

ArrayAdapter, as you can see here, has this available constructors:

ArrayAdapter(Context context, int resource)

ArrayAdapter(Context context, int resource, int textViewResourceId)

ArrayAdapter(Context context, int resource, String[] objects)

ArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects)

ArrayAdapter(Context context, int resource, List<String> objects)

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

So you have to call one of them

and you are calling

super(context, textViewResourceId, itemResourceIds, list); that is ArrayAdapter(Context, int, int[], ArrayList) and does not exists.

instead of an array of int, itemResourceIds should be an int.

How to fix it depends on what are the contents of itemResources[];

think that this:

ArrayAdapter(Context context, int resource,  int textViewResourceId, List<String> objects)

receives the id of an xml layout file, and then the id of a text field.

and this

ArrayAdapter(Context context, int resource, List<T> objects)

receives just the id of the xml file

probably you may want to just call

 super(context, itemResourceIds[0],textViewResourceId,  list); 

or just

 super(context, itemResourceIds[0], list); 

but that depends on what the content of itemResourceIds actually are.

If you are implementing the whole adapter, you dont really care about what id you are passing to the parent, so you can simply change it to

 super(context, 0, list); 

since in the parent class nobody is goint to use it.

Anyways, if you are goint to implement everything by yourself, you can consider extending BaseAdapter instead of ArrayAdaper since BaseAdapter doesnt need any parameter in the constructor, and probably you dont need any functionality of the ArrayAdapter.

OTHER TIPS

That's because there's no constructor that takes the arguments you are providing. In the documentation I don't see any constructor matching your parameters (especially the third parameter, the array)

Have a closer look at the docs. There is no constructor that accepts an int[] as itemResourceId (without "s"). The constructor you probably are looking for is this :

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

Take a look at the documentation: http://developer.android.com/reference/android/widget/ArrayAdapter.html

There is no Constructor in the class ArrayAdapter that takes a int[] as third parameter, try with:

super(context, textViewResourceId, list);

In your code: "super(context, textViewResourceId, itemResourceIds, list);" The "itemResourceIds" the array of integer mismatch, i.e., does not match with any public constructor defiend in ArrayAdapter Class. So please remove it. And if you need the "itemResourceIds" you may store into a member variable. please check this. If please describe the purpose of "itemResourceIds". then I will try to help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top