Here is the code of the fragment in which I am setting a custom adapter to the list.
There no errors but the ListView
is empty. I have implemented
What you have been doing is
In your adapter
public CarListAdapter(Context context , ArrayList<CarDetail> items) {
this.context = context;
this.items = items;
}
in your Fragment
adapter = new CarListAdapter(getActivity(),ServiceCarListFragment.this.carDetailList);
I hope you will be using FragmentActivity
You need to call
adapter = new CarListAdapter(YOUR_ACTIVITY_CONTEXT, carDetailList);
where YOUR_ACTIVITY_CONTEXT
will be your FragmentActivity
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();
I had the same problem. And after trying all tips above my getView was still not being called. So I tried to remove the ScrollView that I used outside the ListView. Then the getView worked well. Just for add one more posibility. I Hope help someone.
You are missing the super class in the constructor. See my example below:
public AppDataAdapter(Activity a, int textViewResourceId, ArrayList<AppData> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
The only reasons getView
is not called are:
getCount
returns 0.setAdapter
on the ListView
.ListView
's visibility (or its container's visibility) is GONE
. Thanks to @TaynãBonaldo for the valuable input.ListView
is not attached to any viewport layout. That is, mListView = new ListView(...)
is used without myLayout.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 than 0
(usually the dataset size)setLayoutManager
and setAdapter
have to be called on the UI Thread
VISIBLE
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