如果我在與我的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. 如何從BroadcastReceiver類更新片段ListView
- 3. 從ActionMode.Callback.onDestroyActionMode()刷新ListView
- 4. 如何使用BroadcastReceiver更新ListView?
- 5. 如何動態刷新ListView?
- 6. 如何使Android ListView刷新
- 7. 如何刷新ListView控件;
- 8. 如何刷新我的ListView?
- 9. 如何在Drag-Sort ListView中刷新ListView?
- 10. ListView不刷新從AsyncTask
- 11. 從其他SherlockFragment刷新ListView
- 12. 從自定義ListView類中刷新ListView
- 13. 如何刷新Android Listview只有新值
- 14. ListView刷新
- 15. android listview刷新
- 16. CursorAdapter ListView刷新
- 17. ListView刷新GetView()
- 18. Nativescript ListView刷新
- 19. .NET Listview刷新
- 20. 刷新Listview從服務器更新
- 21. Android ListView和刷卡刷新
- 22. 我如何從服務器刷新/更新,在我的ListView(CustomAdapter)
- 23. 如何刷新我的ListView與Swip刷新佈局?
- 24. 如何刷新SherlockFragmentActivity的FragmentTabHost中的listview?
- 25. 如何在Android中刷新MainActivity或Listview
- 26. 如何刷新BaseAdapter中的Listview?
- 27. 我如何刷新android中的listview?
- 28. 如何在asynctask後刷新listview android
- 29. 如何使android listview刷新動畫?
- 30. 如何刷新活動中的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