Reputation: 83
I have some boring question. I am beginner of android. I am trying to make Custom Listview. I can not understand this code.
Why use ArrayAdapter<ItemList>
and why pass value super(context, R.layout.activity_custom_adapter)
?
Why use getView()
and why use layout again R.layout.activity_custom_adapter
?
public class CustomAdapter extends ArrayAdapter<ItemList> {
ArrayList<ItemList> item;
Activity context;
public CustomAdapter(Activity context, ArrayList<ItemList> item) {
super(context, R.layout.activity_custom_adapter, item);
this.item=item;
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
if(view==null){
LayoutInflater inflate=context.getLayoutInflater();
view=inflate.inflate(R.layout.activity_custom_adapter, parent, false);
}
return view;
}
Upvotes: 0
Views: 79
Reputation: 67189
Why use
ArrayAdapter<ItemList>
?
This means that your CustomAdapter
is a subclass of ArrayAdapter
, and that it will contain classes only of type ItemList
. All ListViews
need an adapter of some sort to translate plain Java classes into Views
that the ListView
can use as rows in the list.
If you aren't sure what subclasses are or how inheritance works, I highly recommend the What is Inheritance? page from the Java tutorials.
Why pass value super(context, R.layout.activity_custom_adapter)?
The ArrayAdapter
class does a lot of work for you when it comes to creating an adapter for a ListView
. This line calls the constructor for ArrayAdapter
to ensure that all of that class' member variables are initialized properly. Again, I would check out the guide to inheritance that I linked to above.
Why use getView()?
getView()
is a method defined in the root Adapter
class. The @Override
annotation means that we don't want to use the default getView()
implementation provided by any super classes.
Remember that an adapter is responsible for converting plain Java objects into View
instances for the list. Whenever the ListView
needs to generate a View
for another row in the list, it will call getView()
.
Why use layout again
R.layout.activity_custom_adapter
?
This layout is the layout for each individual row in the list. getView()
will return a copy of this layout for each item in the list.
Upvotes: 1