Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,731,559 Members 51,879 Now Online
XDA Developers Android and Mobile Development Forum

ListView in Fragment not displaying

Tip us?
(Last edited by xDandon; 20th January 2014 at 01:14 AM.) Reason: Additional info added (code and logs)
Junior Member - OP
Thanks Meter 0
Posts: 2
Join Date: Jan 2014
Default ListView in Fragment not displaying

Hello, I am hoping someone can point out anything I might be doing wrong that is causing my issue. (seems hard to find "best practice" type of advise for newer android features)

My problem is that a ListView which is inside a fragment, controlled by a ViewPager does not get displayed on the screen if I swipe to the next fragment. (but a TextView inside the same fragment shows up fine on all fragment instances),

To set the scene, I have only one fragment class out of which I instantiate all the fragments used by the ViewPager.
Inside the fragments onActivityCreated I create a cursor loader and a custom cursor adapter and on the loaders onLoadFinished I swap the cursor as its supposed to be. Naturally the customCursorAdapter that I created handles the inflation of the listView row in the newView method and binds the data from the cursor in the bindView method.

Does this sound right? I mean I did it this way with the intent of not having tight coupling, having the fragment be independent. So why won't it draw out the listview for subsequent pages? From what I have seen in the logs, all methods get called for all fragments, including getItem, newView, bindView etc.

Final quirk is that the "previous" fragmet's listView does get drawn. Meaning I start on Fragment 1 - listView is there. Move to fragment 2 - listview not there, move to fragment 3 list view not there, move back to fragment 2 list view IS there now, and back to fragment 1 it is NOT there anymore.

I don't have time to post code right now but will as soon as I get a chance.

Thank you all.

            //main activity which creates the view pager and adapter and starts the process 
            protected void onCreate(Bundle savedInstanceState) {
		Log.d(TAG,"onCreate begin");
		viewPager = (ViewPager) findViewById(;
		FragmentManager fragmentManager = getSupportFragmentManager();
		MyPagerAdapter myPagerAdapter = new MyPagerAdapter(fragmentManager);
		viewPager.setOffscreenPageLimit(0); // trying to force drawing of fragments
		Log.d(TAG,"onCreate viewPager done");

         //MyPagerAdapter getItem (extends FregmentSatePagerAdapter
         public Fragment getItem(int i) {
		Log.d(TAG,"getItem called with item:"+i); 		
		return TaskListFragment.newInstance(i);

        //fragment onActivityCreated , this fragment instanciated in getView of viewPager
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(TAG, "onActivityCreated with datePointer:"+datePointer);
		TextView textViewFragmentTitle = (TextView) getView().findViewById(;
		loaderManager = getLoaderManager();	
		list = (ListView) getActivity().findViewById(;

		customAdapter = new CustomCursorAdapter(getActivity(), null, 1  , datePointer);
		loaderID = datePointer; //attempt to get fragment listview to draw - not working

		loaderManager.initLoader(loaderID, null, this);

       ///cursor loader onFinish
	public void onLoadFinished(<Cursor> loader,
			Cursor newCursor) {
		Log.d(TAG, "onLoadFinished:"+datePointer);
		if (customAdapter != null && newCursor != null ){
			Log.d(TAG, "onLoadFinished swapCursor:"+datePointer);
			//doesnt help customAdapter.notifyDataSetChanged();
		} else {
			Log.v(TAG,"OnLoadFinished: mAdapter is null");

    //MyCustomCursorAdapter (used by the listview inside the fragment above)
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
		View retView = inflater.inflate(R.layout.task_list_item_layout, null);
		RelativeLayout.LayoutParams relativeLayoutParameters;
		Log.d(TAG,"newView layout inflated:"+datePointer);
                return retView;
      ///bindView of same MyCustomCursorAdapter
	public void bindView(View view, Context context, Cursor cursor) {
		TextView tvName = (TextView) view.findViewById(;
	        tvName.setText(cursor.getString(cursor.getColumnIndex(DBHelper.C_TASK_NAME))+" ["+datePointer+"]");

Also check out the LogCat logs with description of the action in the name corresponding to the log statements in the code above.

I noticed more detail about what happens (if you look at the logcat you will see also)
When the screen is displayed the first fragment has the listview present, but the fragment has the id/datePointer (same thing) of "0" BUT the listview items have an id/datePointer of "1"

If you look at the initial logcat you will see "newView layout inflated:1" but no "newView layout inflated:0" how this is being skipped I have no idea. And how the id's can be mix-matched between fragment instances baffles me.

And lastly, after scrolling right twice (to the 0,1, 2nd fragment) it did not have the listview as reported, but scrolling BACK ONE the listview shows up in the 1st fragment ... but even more baffling with lisvtView items that report being part of the "0" fragment .... whaaaat?
EatHeat's Avatar
Recognized Developer
Thanks Meter 1389
Posts: 911
Join Date: Jan 2013
Location: Calcutta
Why are you using a listview inside fragment when you have listfragment available?
Customize your clock.

[2.2+]SnowBowl HD.
Enjoy the vacations!

(Last edited by xDandon; 22nd January 2014 at 12:59 PM.) Reason: solved
Junior Member - OP
Thanks Meter 0
Posts: 2
Join Date: Jan 2014
Originally Posted by EatHeat View Post
Why are you using a listview inside fragment when you have listfragment available?
EatHeat, I am doing this for the sake of flexibility for adding additional controls/views to the fragment besides just a list view (Example I envision adding several buttons to the bottom of the fragment/screen which will be "outside" the listview.

Does it make more sense what I am doing now? That is exactly one of the questions I have, its so hard to find advise on what makes more sense when it comes to these complex combinations of viewPager+fragments+custom listViews+loaders

So my current flow is:
1--Activity (creates ViewPager with myPagerAdapter)
2----ViewPager in itst getItem method instantiates a fragment from class TaskListFragment
3-------The fragment in it's onActivityCreated instantiates a CustomCursorAdapter (with null cursor) and initializes a loader
4-------The loader swaps the cursor in it's onLoadFinished (and this is supposed to populate the listview)

Items 2-4 repeat for every "swipe" or every fragment instantiated.

-The listView control XML is defined in an XML file which is used by the fragment's setContentView
-The lisView row layout XML is defined in a separate XML file inflated by the customCursorAdapter's newView method.

SOLVED: (after literally one week of headache and rewriting the code in every way I know)

the fragments onCreateView I had it getting the listview with getActivity() .. but I should have done it with getView()

the solution was replacing this line
list = (ListView) getActivity().findViewById(;
with this line
list = (ListView) getView().findViewById(;
That was it!!! all the headache!!

I don't particularly understand what getView does as opposed to getActivity .. but right now I am pleased it works.
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes