如果我在與我的ListView關聯的自定義適配器上調用notifyDataSetChanged()
,則所有視圖都應刷新自己(將調用getView()
)。 現在我有一個正在監聽事件的BroadcastReceiver。事件觸發時,必須刷新ListView。我怎樣才能做到這一點? 謝謝!如何從BroadcastReceiver刷新ListView?
4
A
回答
2
如果從接收器刷新列表視圖你有這樣的代碼:
BroadcastReceiver br;
public final static String BROADCAST_ACTION = "BROADCAST_ACTION";
br = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//code refreshing...
}
};
IntentFilter intFilt = new IntentFilter(BROADCAST_ACTION);
registerReceiver(br, intFilt);
你的代碼調用它:
Intent intent = new Intent(BROADCAST_ACTION);
sendBroadcast(intent);
如果您需要刷新是另一個動作你只是需要添加(行動後):
Intent intent = new Intent(BROADCAST_ACTION);
sendBroadcast(intent);
+0
它無法正常工作。請詳細說明您的答案。 – 2015-10-26 07:13:24
1
根據要求,請看下面的示例代碼:
public interface OnDataUpdateListener {
void onDataAvailable(ArrayList<String> newDataList);
}
public class MyTestReceiver extends BroadcastReceiver {
public static final String DATA_LIST = "DATA_LIST";
private OnDataUpdateListener mDataUpdateListener = null;
public MyTestReceiver(OnDataUpdateListener dataUpdateListener) {
mDataUpdateListener = dataUpdateListener;
}
@Override
public void onReceive(Context ctx, Intent intent) {
// assuming data is available in the delivered intent
ArrayList<String> dataList = intent.getSerializableExtra(DATA_LIST);
if (null != mDataUpdateListener) {
mDataUpdateListener.onDataAvailable(dataList);
}
}
}
public class MyActivity extends FragmentActivity implements OnDataUpdateListener {
public static final String ACTION_DATA_UPDATE_READY = "ACTION_DATA_UPDATE_READY";
private MyTestReceiver mTestReceiver = null;
private <SomeAdapterClass> mAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// other required initialization
mTestReceiver = new MyTestReceiver(this);
}
@Override
public void onResume() {
super.onResume();
if (null != mTestReceiver) {
registerReceiver(mTestReceiver, new IntentFilter(ACTION_DATA_UPDATE_READY));
}
}
void onDataAvailable(ArrayList<String> newDataList) {
// assuming you want to replace existing data and not willing to append to existing dataset
mAdapter.clear();
mAdapter.addAll(newDataList);
mAdapter.notifyDataSetChanged();
}
}
0
在你的數據被更新,斷火的消息信令數據的代碼已被更改... (您需要訪問上述活動或應用程序上下文來做到這一點)
Intent intent = new Intent("ListViewDataUpdated");
LocalBroadcastManager.getInstance(context.sendBroadcast(intent));
然後,只需在您的活動使用下面的代碼趕上抓的消息,並告訴你ListAdapter更新...
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
myListAdapter.notifyDataSetChanged();
}
};
@Override
public void onResume(){
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("ListViewDataUpdated"));
myListAdapter.notifyDataSetChanged();//in case our data was updated while this activity was paused
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
來源:改編自Vogella
相關問題
- 1. 如何從BroadcastReceiver類更新片段ListView
- 2. 如何動態刷新ListView?
- 3. 如何刷新我的ListView?
- 4. 如何在Drag-Sort ListView中刷新ListView?
- 5. 如何刷新Android Listview只有新值
- 6. .NET Listview刷新
- 7. ListView刷新GetView()
- 8. android listview刷新
- 9. CursorAdapter ListView刷新
- 10. 刷新Listview從服務器更新
- 11. Android ListView和刷卡刷新
- 12. 如何刷新我的ListView與Swip刷新佈局?
- 13. 如何刷新其適配器的listView
- 14. 如何在android中刷新listview?
- 15. 如何強制listview刷新其視圖?
- 16. 如何在ViewPager中刷新Android ListView?
- 17. 如何使用BaseAdapter刷新我的ListView
- 18. 我如何刷新android中的listview?
- 19. 刷新ListView與SwipeRefreshLayout
- 20. 創建listView刷新
- 21. Android - ListView不刷新
- 22. Wicket ListView不刷新
- 23. Android:如何從靜態BroadcastReceiver更新UI?
- 24. listView不會刷新新值
- 25. 如何從Apache Wicket的另一個ListView刷新面板?
- 26. 如何在listview刷新時保持listview中的文本框值?
- 27. 如何從AsyncTask更新ListView?
- 28. 使用ArrayAdapter刷新ListView
- 29. Android Swipe Up刷新ListView
- 30. ProgressDialog,同時刷新ListView
「現在我有一個正在監聽事件的BroadcastReceiver」 - 事件是什麼,「BroadcastReceiver」是如何註冊的(manifest或registerReceiver())? – CommonsWare 2014-10-02 14:30:40
嗨,在包含ListView的Activity中進行了dinamically註冊,事件是從DownloadManager中ACTION_DOWNLOAD_COMPLETE。 – Angelo 2014-10-02 14:31:56
在onReceive的BroadcastReceiver實現中,您必須:修改您的自定義適配器的底層數據並調用notifyDataSetChanged或創建新的自定義適配器並將其設置爲列表視圖 – Selvin 2014-10-02 14:35:47