The constructor ArrayAdapter<String> is undefined - super() marked - android

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.

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.

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.

Related

Error in initialization of custom array adapter

I am trying to make a custom listview. The list is declared as below
List<DocRow> doctors = new ArrayList<>();
This list is then being populated.
My custom array adapter is in a separate class with its constructor declared as below.
public class DocAdapter extends ArrayAdapter<DocRow>{
Context context;
int resource;
ArrayList<DocRow> doctors;
private LayoutInflater inflater;
public DocAdapter(#NonNull Context context, #LayoutRes int resource, ArrayList<DocRow> doctors) {
super(context, resource, doctors);
this.context = context;
this.resource = resource;
this.doctors = doctors;
inflater = LayoutInflater.from(context);
}
Now in my main activity, I am trying to create a new custom array adapter by passing off my list (which is a valid parameter), it isn't accepted. The code for creation and setting of adapter for linking the listview with the list is below.
DocAdapter adapter = new DocAdapter(getApplicationContext(), R.layout.doc_row, doctors);
docList.setAdapter(adapter);
Can anyone explain what is the issue? The link for error screenshot is above. I tried searching for this specific issue, but haven't been able to find a solution that works.
Change your constructor argument to List instead of ArrayList as you are passing list in it.
List<DocRow> doctors;
public DocAdapter(#NonNull Context context, #LayoutRes int resource, List<DocRow> doctors) {
super(context, resource, doctors);
this.context = context;
this.resource = resource;
this.doctors = doctors;
inflater = LayoutInflater.from(context);
}
As pointed by #Tim, here is a little detail about why this is needed.
When an instance is initialized, it may be initialized with one of its child classes but the object remains an instance of Super class only(Due to runtime polymorphism) and therefore the methods that consume this instance either expect super class or the instance should be casted to superclass before passing it on.
The easiest way to identify is to always look at the type on the left-hand side instead.
List a=new ArrayList();
In above example, the instance is actually an arraylist but it is of Type List.
A parent class's reference can store subclass's object, but the reverse is not true.
Here, in the constructor of your adapter, you have ArrayList<DocRow> as your parameter type, but your doctors list is of type List<DocRow>. You, you're passing a List<> object to an ArrayList<> reference.
To solve it, either change your doctors variable type to ArrayList<>, or your constructor parameter type to List<>

"ArrayAdapter requires the resource ID to be a TextView"

I basically know where this exception comes from and what causes it. It's because my textview is wrapped in an RelativeLayout.
But can you tell me why this works
playerAdapter = new PlayerAdapter(this,R.layout.item_lv_player,playerList);
lvPlayer.setAdapter(playerAdapter);
While this doesn't
PlayerAdapter playerAdapter1 = new PlayerAdapter(this,R.layout.item_lv_player,playerAdapterSource[0]);
spinnerPlayer1.setAdapter(playerAdapter1);
Shouldn't both of this throw an exception?
PlayerAdapater is a class that extends ArrayAdapter.
Is it because in the first example I'm using a ListView, and in the second one it's a Spinner? That wouldn't sound logical to me at all since the problem is the creation of the Adapter.
exception is not depends upon your adapter,its depends upon widgets.Every widgets has some properties,based on that only it will raise a exception,
Listview always depends upon Textview for all positions,but spinner not depending on that.If you run ListView without R.id.TextView it raise a nullpointer exception because of you have not initialized the textview in the adapter.But spinner doesnot because it not depending on the textview.
You have ArrayAdapter with 3 parameters passed to it.
The 3 parameter ArrayAdapter constructor can take any of the following parameters -
1) ArrayAdapter(Context context, int resource, int textViewResourceId)
2) ArrayAdapter(Context context, int resource, T[] objects)
The first one Explanation -
public ArrayAdapter (Context context, int resource, int
textViewResourceId)
Added in API level 1 Constructor
Parameters
context The current context.
resource The resource ID for a layout file containing a layout to
use when instantiating views.
textViewResourceId The id of the TextView within the layout
resource to be populated
The second one Explanation -
public ArrayAdapter (Context context, int resource, T[] objects)
Added in API level 1 Constructor
Parameters
context The current context.
resource The resource ID for a layout file containing a TextView to use when instantiating views.
objects The objects to represent in the ListView.
Having explained that for -
PlayerAdapter playerAdapter1 = new PlayerAdapter(this,
R.layout.item_lv_player,playerAdapterSource[0]);
spinnerPlayer1.setAdapter(playerAdapter1);
Is the last parameter a textViewResourceId ? > NO
Is the last parameter a ListView ? > NO
From developer docs android ArrayAdapter.

How to inherit from ArrayAdapter?

So I found some code that I am trying to get working. I am new to Android/Exclipse/Java and still trying to wrap my head around the whole thing. However, I am starring myself blind.
Say I have this:
private class RSSListAdapter extends ArrayAdapter<MyRSSItem> {
private List<MyRSSItem> objects = null;
public RSSListAdapter(Context context, int viewid, List<MyRSSItem> objects) {
super(context, viewid, objects);
this.objects = objects;
}
}
and then elsewhere this:
myRssAdapter = new RSSListAdapter(thisActivityContext, newsListView, myItemsDataArrayList);
where myItemsDataArrayList is declared like this:
ArrayList<MyRSSItem> myItemsDataArrayList = new ArrayList<MyRSSItem>();
I get this error:
The constructor News.RSSListAdaptor(Context, ListView, ArrayList) is undefined
Of course I have tried Google, but, well, no luck.
The constructor's definition is this:
public RSSListAdapter(Context context, int viewid, List<MyRSSItem> objects) {
This means it accepts in only a Context, an int, and a List <MyRSSItem>
However, you call the constructor with these arguments:
new RSSListAdapter(thisActivityContext, newsListView, myItemsDataArrayList);
the second argument is a newsListView, which isn't an int, it is a ListView. They are not the same type, hence the compile-time error.
If you change to android.R.id.text1, it should work, since that is a valid TextView id, like the superclass constructor requires.
And definitely don't forget to set the ListView's adapter to your RSSListAdapter after creating it.
For more detailed info, this tutorial is pretty helpful.

Regarding Activity.getApplicationContext()

I have created a custom adapter for a list view. In the constructor of the adapter i do the following:
public CustomListAdapter(Context context, int textViewResourceId,
ArrayList<CatalogueItemListData> items,int width) {
super(context, textViewResourceId, items);
this.context =context;
mInflater = LayoutInflater.from(context);
}
In my activity class i call the adaper as below:
this.m_adapter = new CustomListAdapter(this,
android.R.layout.simple_list_item_1, m_orders,displayMetrics.widthPixels);
listView.setAdapter(this.m_adapter);
and i get the output as in the image below:
Now when i change the constructor of my adapter as below:
public CustomListAdapter(Activity activity, int textViewResourceId,
ArrayList<CatalogueItemListData> items,int width) {
super(activity.getApplicationContext(), textViewResourceId, items);
this.context =activity.getApplicationContext();
mInflater = LayoutInflater.from(context);
}
I get the following output:
Could someone kindly tell me what could be the possible reason for the difference in the outputs? Thanks in advance.
I would say that you have applied a different theme to your specific activity then the entire application. This could cause the difference in styles based on which context is used. I am not sure if this is what is happening but it would make sense.
Your application context will use previously defined theme (or default theme) where was your activity context will not unless you call it explicitly.
Hope this helps!

How do i put a vector into my arrayadapter?

I have the current code:
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_spinner);
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
colourSpinner.setAdapter(adapter);
How would i put a vector into my array_spinner?
Thank you.
The Vector class is considered deprecated, don't use it. Use a List instead. Then you can use the overloaded ArrayAdapter constructor:
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

Categories

Resources