Android onResume后ListView为空
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将
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.