Search code examples
androidandroid-listviewandroid-cursoradapterandroid-cursorloader

CursorAdapter.swapCursor not refreshing the first time


I have a ListActivity and use a CursorLoader to load the data which is stored in a ContentProvider. I use a custom class extending CursorAdapter to display the list items. In the LoaderCallbacks onLoadFinished I have the following:

public void onLoadFinished(Loader<Cursor> loader, Cursor newCursor) {
   cursorAdapter.swapCursor(newCursor);
}

I have a custom layout for the ListActivity which includes a TextView with android:id="@android:id/empty".

The problem is that when I open the application for the first time calling swapCursordoes not refresh the ListView even though there is data to show in the ContentProvider. When I add a new item to the ListView, the list is refreshed properly. However, if I comment out the TextView, which displays a simple text when no data is available, the application works as expected. The swapCursor call automagically updates the ListView accordingly.

Any thoughts on why this occurs or if there is a proper way to do this since I believe calling notifyDataSetChanged won't do the work as the refreshing fails on a very particular case?


Solution

  • You're having this problem because ListActivity automatically sets the empty view (if available) to your ListView.

    I'd suggest you try one of these:

    1. Extend activity and after swapCursor call

      listView.setEmptyView(findViewById(android.R.id.empty);
      
    2. Make the empty view gone: android:visibility="gone" and after swapCursor call

      findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
      
    3. @onCreate call listView.setEmptyView(null) and after swapCursor call

      listView.setEmptyView(findViewById(android.R.id.empty);
      

    Not sure about all of them, but one of em will certainly work :)