【问题标题】:Android update ListFragment's listView from another fragmentAndroid 从另一个 Fragment 更新 ListFragment 的 listView
【发布时间】:2015-06-07 23:53:50
【问题描述】:

我有一个关于从我的地图片段更新 ListFragment 中的 ListView 的问题。每次调用此方法时,我都会为字符串数组分配不同的值。我应该在 Update 方法中添加什么来刷新列表视图?

【问题讨论】:

  • 使用广播接收器
  • 你能发个sn-p什么的吗?

标签: android android-fragments android-listview


【解决方案1】:

在 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 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); 

【讨论】:

  • 嗯。谢谢,但它没有回答我的问题。我想知道如何做那部分//用你的数据做一些事情,例如如何刷新 listFragment
猜你喜欢
  • 1970-01-01
  • 2019-01-10
  • 2014-04-18
  • 2014-04-06
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多