Listview With Custom Adapter
I followed several tutorials but I still can't populate my list view. What am I doing wrong? this is the layout spaced_list.xml
Solution 1:
Change this:
public CategoriesAdapter(Context context, ArrayList<Category> categories){
super(context, 0); // <- Wrong constructorthis.context = context;
this.categories = categories;
}
To this:
public CategoriesAdapter(Context context, ArrayList<Category> categories){
super(context, 0, categories);
this.context = context;
this.categories = categories;
}
You are currently calling this constructor of the super class:
publicArrayAdapter(Context context, int textViewResourceId) {
init(context, textViewResourceId, 0, new ArrayList<T>());
}
Which will eventually create a new ArrayList
and ignore the data you passed in.
Solution 2:
The problem is in the constructor in your adapter. You need to provide your ArrayList<Category>
when you call super()
. Here is the code:
public CategoriesAdapter(Context context, ArrayList<Category> categories){
super(context, 0, categories); //Providing objects to represent in the ListViewthis.context = context;
this.categories = categories;
}
This is the documentation with the reference for this ArrayAdapter constructor.
Post a Comment for "Listview With Custom Adapter"