2014-10-02 82 views
4

如果我在與我的ListView關聯的自定義適配器上調用notifyDataSetChanged(),則所有視圖都應刷新自己(將調用getView())。 現在我有一個正在監聽事件的BroadcastReceiver。事件觸發時,必須刷新ListView。我怎樣才能做到這一點? 謝謝!如何從BroadcastReceiver刷新ListView?

+0

「現在我有一個正在監聽事件的BroadcastReceiver」 - 事件是什麼,「BroadcastReceiver」是如何註冊的(manifest或registerReceiver())? – CommonsWare 2014-10-02 14:30:40

+0

嗨,在包含ListView的Activity中進行了dinamically註冊,事件是從DownloadManager中ACTION_DOWNLOAD_COMPLETE。 – Angelo 2014-10-02 14:31:56

+1

在onReceive的BroadcastReceiver實現中,您必須:修改您的自定義適配器的底層數據並調用notifyDataSetChanged或創建新的自定義適配器並將其設置爲列表視圖 – Selvin 2014-10-02 14:35:47

回答

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