2016-07-05 41 views
0

我知道,爲了更新列表視圖,我必須使用notifyDataSetChanged()。但是,我怎樣才能實時更新自定義ListView?Android - 每分鐘實時更新自定義列表查看

例如,我的應用在05h:10m:20s創建了Listview,40秒後它應該每隔一分鐘更新一次Listview。換句話說,它應該確定當前時間是否已經改變了一分鐘而不是更新Listview。我怎樣才能做到這一點?

+0

你可以使用處理器用於這一目的的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分鐘) –

回答

3

我想你想的每一分鐘,在第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

+0

是的,那正是我正在尋找的。謝謝! – gigs

0

你應該是這樣的:

private ListView listView; 

final Runnable r = new Runnable() { 
    public void run() { 
     adapter.notifyDataSetChanged(); 
     listView.postDelayed(this, 60000); 
    } 
}; 

listView.postDelayed(r, 60000); 
0

工作得很好!

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