Custom Adapter getView() method is not called
Solution 1:
The only reasons getView
is not called are:
-
getCount
returns 0. - you forget to call
setAdapter
on theListView
. - If the
ListView
's visibility (or its container's visibility) isGONE
. Thanks to @TaynãBonaldo for the valuable input. -
ListView
is not attached to any viewport layout. That is,mListView = new ListView(...)
is used withoutmyLayout.addView(mListView)
.
In the onPostExcute
, after you create a new instance of CarListAdapter
I will suggest you to update the new instance to your ListView
. Indeed you need to call again
mList.setAdapter(adapter);
Edit: setAdapter
should be always called on the ui thread, to avoid unexpected behaviours
Edit2:
The same applies to RecyclerView
. Make sure that
-
getItemCount
is returning a value grater than0
(usually the dataset size) - both
setLayoutManager
andsetAdapter
have to be called on theUI Thread
- The visibility of the widget has to be set to
VISIBLE
Solution 2:
you must verify that the list has elements might have an error when adding items to your list . To verify , use the method:
adapter.getCount();
Solution 3:
I faced similar problem. Here is a simple work around to solve it:
In your onCreateView, you will have to wait before the view gets created. So change your lines from this:
mList = (ListView)v.findViewById(R.id.list);
mList.setAdapter(adapter);
CHANGE THE ABOVE TWO LINES INTO:
mList = (ListView)v.findViewById(R.id.list);
mList.post(new Runnable() {
public void run() {
mList.setAdapter(adapter);
}
});
Hope this will help others who would run into similar problem