Android onResume后ListView为空

发布于 2024-10-31 18:19:39 字数 1866 浏览 5 评论 0原文

我有一个 ListView,在 onResume 上我试图用 SQL Lite 数据库中的数据填充它。在恢复期间,我添加了一个单击侦听器并重新填充列表视图。 onResume、populateListView() 和 onPause 方法如下。完整的源代码可在 GitHub 上找到

onResume:

@Override
protected void onResume() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(TalkServerIntent.INTENT_NEW_MESSAGES.toString());
    registerReceiver(receiver, filter);

    setContentView(R.layout.main);

    talkListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parentView, View childView,
                            int position, long id) {
            GeoCamTalkMessage msg = adapter.getTalkMessage(position);
            if (msg.hasAudio()) {
                try {
                    UIUtils.playAudio(getApplicationContext(), msg, player,
                            siteAuth);
                } catch (Exception e) {
                    UIUtils.displayException(getApplicationContext(), e,
                            "Cannot retrieve audio");
                }
            }
        }
    });

    populateListView();
    super.onResume();
}

populateListView:

private void populateListView() {
    try {
        talkMessages = messageStore.getAllMessages();
    } catch (Exception e) {
        Log.i("Talk", "Error:" + e.getMessage());
    }

    if (talkMessages != null) {
        adapter.setTalkMessages(talkMessages);
        talkListView.setAdapter(adapter);
    }
}

我没有在 onPause 上做任何特殊的事情,但由于这些值存储在数据库中,我认为我不必这样做。

onPause

@Override
protected void onPause() {
    unregisterReceiver(receiver);
    super.onPause();
}

我做错了什么导致 ListView 无法在 Resume 上刷新?

I have a ListView which onResume I am trying to populate with data from a SQL Lite Database. During resume I add a click listener and repopulate the List View. The onResume, populateListView(), and onPause methods are below. The full source code is available on GitHub.

onResume:

@Override
protected void onResume() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(TalkServerIntent.INTENT_NEW_MESSAGES.toString());
    registerReceiver(receiver, filter);

    setContentView(R.layout.main);

    talkListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parentView, View childView,
                            int position, long id) {
            GeoCamTalkMessage msg = adapter.getTalkMessage(position);
            if (msg.hasAudio()) {
                try {
                    UIUtils.playAudio(getApplicationContext(), msg, player,
                            siteAuth);
                } catch (Exception e) {
                    UIUtils.displayException(getApplicationContext(), e,
                            "Cannot retrieve audio");
                }
            }
        }
    });

    populateListView();
    super.onResume();
}

populateListView:

private void populateListView() {
    try {
        talkMessages = messageStore.getAllMessages();
    } catch (Exception e) {
        Log.i("Talk", "Error:" + e.getMessage());
    }

    if (talkMessages != null) {
        adapter.setTalkMessages(talkMessages);
        talkListView.setAdapter(adapter);
    }
}

I'm not doing anything special on the onPause, but since the values are stored in the database didn't think I would have to.

onPause

@Override
protected void onPause() {
    unregisterReceiver(receiver);
    super.onPause();
}

What am I doing wrong that is causing the ListView to not be refreshed onResume?

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

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

发布评论

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

评论(1

江湖正好 2024-11-07 18:19:39

您可以尝试将 setContentView(R.layout.main); 移动到 onCreate() 方法。

每次在 onResume() 中设置它可能会导致问题。

You could try moving setContentView(R.layout.main); to your onCreate() method.

Setting it in onResume() every time might cause issues.

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