Update ListView after initialising CursorAdapter
I have a Tabs Activity that contains Fragments for tabs. One of the tabs displays a list of data from a SQLite databse table (or a different Fragment if the table is empty). When initially creating the tabs, the Tabs Activity checks to see if the table contains any data. If it does, then it initialises the list Fragment but doesn't set up the CursorAdapter. The CursorAdapter is initialised by an AsyncTask which syncs the SQLite database with a central database, and then creates the Cursors and CursorAdapters. The list Fragment displays a ProgressDialog while waiting for the AsyncTask to create the CursorAdapter. When the CurserAdapter is initialised, the ProgressDialog is dismissed but the ListView remains on its 'empty list' view, despite calls to notifyDataSetChanged(). If I switch tabs and come back, the ListView displays the data correctly. How can I make the ListView update once the CursorAdapter has been initialised?
Relevant bits of code:
Tabs:
private static ImageCursorAdapter friendCursorAdapter = null;
public static ImageCursorAdapter getFriendsListAdapter() {
return friendCursorAdapter;
}
public static void setFriendsListAdapter(ImageCursorAdapter adapter) {
friendCursorAdapter = adapter;
friendCursorAdapter.notifyDataSetChanged();
}
SyncTask:
protected Void doInBackground(Void... params) {
sql = "SELECT COUNT(*) FROM " + WhereWolfOpenHelper.FRIEND_TABLE_NAME;
statement = db.compileStatement(sql);
count = statement.simpleQueryForLong();
if(count>0) {
friendCursor = db.query(WhereWolfOpenHelper.FRIEND_TABLE_NAME, null, null, null, null, null, WhereWolfOpenHelper.FRIEND_FIRST_NAME_COLUMN+", "+WhereWolfOpenHelper.FRIEND_LAST_NAME_COLUMN);
}
statement.close();
}
@Override
protected void onPostExecute(Void param) {
if(friendCursor!=null) {
ImageCursorAdapter adapter = new ImageCursorAdapter(WhereWolfActivity.this, friendCursor, 0, ImageCursorAdapter.FRIENDS);
Tabs.setFriendsListAdapter(adapter);
}
}
FriendsList:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(Tabs.getFriendsListAdapter());
}
@Override
public void onStart() {
super.onStart();
if(Tabs.getFriendsListAdapter()==null) {
final ProgressDialog dialog = ProgressDialog.show(getActivity(), getString(R.string.loadingtitle), getString(R.string.loading), true, false);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while(Tabs.getFriendsListAdapter()==null) {}
dialog.dismiss();
Tabs.getFriendsListAdapter().notifyDataSetChanged();
}
});
thread.start();
}
}
You are creating a new adapter, but I don't see any code to set the adapter to the list. Since the list knows nothing about your brand new adapter, calling notifyDataSetChanged()
doesn't help. BTW, if you are already using fragments, you might consider using a Loader.