I am following a book’s examples to learn Android and came accross this code:
public class DynamicDemo extends ListActivity {
TextView selection;
private static final String[] items={"lorem", "ipsum", "dolor",
"sit", "amet"}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new IconicAdapter());
selection=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent, View v,
int position, long id) {
selection.setText(items[position]);
}
class IconicAdapter extends ArrayAdapter<String> {
IconicAdapter() {
super(DynamicDemo.this, R.layout.row, R.id.label, items);
}
public View getView(int position, View convertView,
ViewGroup parent) {
View row=super.getView(position, convertView, parent);
ImageView icon=(ImageView)row.findViewById(R.id.icon);
if (items[position].length()>4) {
icon.setImageResource(R.drawable.delete);
}
else {
icon.setImageResource(R.drawable.ok);
}
return(row);
}
}
}
I actually understand the code except for in the constructor why do we say:
super(DynamicDemo.this, R.layout.row, R.id.label, items);
That is the only part that is confusing me.
Thanks!
R
There are multiple things going on in there.
First off, that is a
supercall, so it is calling theArrayAdapter‘s constructor. It is calling this constructor: http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, int, T[])The first parameter is a use of
thisthat actually gets the current instance of the DynamicDemo class. This is used since DynamicDemo extends ListActivitty which extends Activity which eventually extends Context, the type of object you need there.The next two parameters are resource IDs which it asks for.
The last parameter is an array it needs to populate the adapter.