I'm writing my own adapter for a ListView, but I'm having trouble with some concepts. First of all, that's what I wrote:
public class MyArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final int parent;
private final String[] values;
public MyArrayAdapter(Context context, int resourceid, String[] values) {
super(context, -1, values);
this.context = context;
this.parent = resourceid;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText("abcde lorem ipsum");
String s = values[position];
imageView.setImageResource(R.drawable.no);
return rowView;
}
}
As you guys can see, I added the resourceid as an argument in the constructor, because this page tells me that it must take 3 arguments. However, at the 3.3 example of this article, when it writes a custom adapter, it uses 2 arguments for the constructor. But if I don't pass R.layout.rowlayout
as an argument in the constructor, how getView know who is the ViewGroup parent
? I mean, somehow getView must be called, and the parent object must be passed to it. But how the class knows who is the parent?
Also, the android page instructs me to call it like this:
MyArrayAdapter<String> adapter = new MyArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myStringArray);
But I end up having to take <String>
off and leave it like this:
MyArrayAdapter adapter = new MyArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myStringArray);
Why?
Is it because the article extended the class without using <String>
? Like this:
public class MyArrayAdapter extends ArrayAdapter<String> {
Also, since I'm overriding the class, why do I need to call the original constructor using super
?