0

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.

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer, but I would really like to grap the parameters the way I have them at the moment. Is there no way to make this possible? By making an additional constructor for example?
your constructor is ok. but if in the itemResources ids you have an array, you need to know what represents every item of the array, so you can pass to the parent the correct one, . look at my update
I'm sorry, but I still don't get why I can't simply pass an array instead of an int. When I call my adapter, I pass the array and get all the itemResourceIds out of it which I use in getView(). So don't I know what represents every item?
the parent class is expecting only an unique field, the same for al the items in the view. thats why super() expects an int instead of an array. you can keep your constructor, and since you are implementing your own getView, it doesnt matter what id you pass to the parent, so you can simply change it to super(context,0, list); since nobody is going to use that id in the super.
Ooooh, super must not be connected to my contructor? Well, that changes everything. I thought you need to have the same parameters there. Thank you very much for your patience and you very detailed answer! :) (code works btw)
|
0

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)

Comments

0

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)

Comments

0

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);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.