Android onResume ListView为空后

时间:2011-04-12 23:36:15

标签: android listview android-activity onresume onpause

我有一个ListView,其中onResume我试图填充来自SQL Lite数据库的数据。在恢复期间,我添加了一个单击侦听器并重新填充列表视图。 onResume,populateListView()和onPause方法如下所示。完整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);
    }
}

我没有对onPause做任何特别的事情,但由于这些值存储在数据库中,所以我认为不会这样做。

的onPause

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

我做错了什么导致ListView不能刷新onResume?

1 个答案:

答案 0 :(得分:5)

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

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