如果我在与ListView关联的自定义适配器上调用notifyDataSetChanged()
,则所有视图都应自行刷新(getView()
将被调用)。
现在我有一个正在收听事件的BroadcastReceiver。触发事件时,必须刷新ListView。我怎样才能做到这一点?
谢谢!
答案 0 :(得分:2)
如果您从接收器刷新listview,您将拥有以下代码:
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);
答案 1 :(得分: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();
}
}
答案 2 :(得分:0)
在更新数据的代码中,触发一条消息,表明数据已被更改... (您需要访问Activity或Application上下文才能执行此操作)
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
答案 3 :(得分:0)
LocalBroadcastManager.getInstance(context.sendBroadcast(intent));
改为
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
我可能错了,但它对我有用...