我知道,为了更新列表视图,我必须使用notifyDataSetChanged()
。但是,我怎样才能实时更新自定义ListView?Android - 每分钟实时更新自定义列表查看
例如,我的应用在05h:10m:20s创建了Listview,40秒后它应该每隔一分钟更新一次Listview。换句话说,它应该确定当前时间是否已经改变了一分钟而不是更新Listview。我怎样才能做到这一点?
我知道,为了更新列表视图,我必须使用notifyDataSetChanged()
。但是,我怎样才能实时更新自定义ListView?Android - 每分钟实时更新自定义列表查看
例如,我的应用在05h:10m:20s创建了Listview,40秒后它应该每隔一分钟更新一次Listview。换句话说,它应该确定当前时间是否已经改变了一分钟而不是更新Listview。我怎样才能做到这一点?
我想你想的每一分钟,在第0秒更新ListView
如果没有,你应该使用其他的答案。
如果是这样,您可以尝试注册广播接收器为ACTION_TIME_TICK
。
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK);
Context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
adapter.notifyDataSetChanged();
}
}, intentFilter);
不要忘记你的Activity
是的,那正是我正在寻找的。谢谢! – gigs
你应该是这样的:
private ListView listView;
final Runnable r = new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
listView.postDelayed(this, 60000);
}
};
listView.postDelayed(r, 60000);
工作得很好!
private ListView listView;
private ListViewAdapter listViewAdapter;
private Handler mainHandler;
//....
//Call method when start updating
public void updateListShedule() {
if (handler == null) {
handler = new Handler(getApplicationContext().getMainLooper())
}
handler.postDelayed (new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
updateListShedule();
}
}, 10000);
}
你可以使用处理器用于这一目的的
onStop()
调用Context.unregisterReceiver()
。 请参阅[http://stackoverflow.com/questions/12916084/android-update-listview-items-every-1-minute](http://stackoverflow.com/questions/12916084/android-update-listview-items-每1分钟) –