初始化 CursorAdapter 后更新 ListView

发布于 2024-11-29 05:36:36 字数 2429 浏览 3 评论 0原文

我有一个选项卡活动,其中包含选项卡片段。其中一个选项卡显示来自 SQLite 数据库表(如果表为空,则显示不同片段)的数据列表。最初创建选项卡时,选项卡活动会检查表是否包含任何数据。如果是,则它会初始化列表 Fragment 但不设置 CursorAdapter。 CursorAdapter 由 AsyncTask 初始化,该任务将 SQLite 数据库与中央数据库同步,然后创建 Cursors 和 CursorAdapter。列表 Fragment 在等待 AsyncTask 创建 CursorAdapter 时显示 ProgressDialog。当 CurserAdapter 初始化时,ProgressDialog 将被关闭,但 ListView 仍保留在其“空列表”视图上,尽管调用了 notificationDataSetChanged()。如果我切换选项卡并返回,ListView 会正确显示数据。 CursorAdapter 初始化后如何更新 ListView?

相关代码:

选项卡:

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();         
    }
}

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();         
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2024-12-06 05:36:36

您正在创建一个新适配器,但我没有看到任何将适配器设置到列表中的代码。由于该列表对您的全新适配器一无所知,因此调用 notifyDataSetChanged() 没有帮助。顺便说一句,如果您已经在使用片段,您可能会考虑使用加载器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文