Android从另一个片段更新ListFragment的listView

时间:2015-06-07 15:26:14

标签: android android-fragments android-listview

我有一个关于从我的Map片段更新ListFragment中的ListView的问题。每次我调用此方法时,我都会为String Array分配不同的值。我应该在Update方法中添加什么来刷新listview?

1 个答案:

答案 0 :(得分:0)

在ListFragment中创建并注册BroadcastReceiver

static final String ACTION_CUSTOM = "action";
BroadcastReceiver mReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   mReceiver = new BroadcastReceiver(){
        public void onReceive(android.content.Context context, Intent intent) {

            //Check for action you need
            if (intent.getAction() == ACTION_CUSTOM ){
                //Do stuff with data from intent
                intent.getExtras().getString("data");
             }
        }
    };
}

@Override
public void onResume() {
    super.onResume();
    getActivity().registerReceiver(receiver, new IntentFilter(ACTION_CUSTOM ));
}


@Override
protected void onPause() {
     super.onPause();
     getActivity().unregisterReceiver(receiver);
} 

在MapFragment中发送广播意图,当您需要更改数据时:

Intent intent = new Intent();

//Set action you need to send
intent.setAction(ACTION_CUSTOM);

//Add data to send
intent.putExtra("data", "data_value");

//Send broadcast intent
getActivity().sendBroadcast(intent);