简体   繁体   English

从一个片段发送数据以在另一个片段的列表视图中显示

[英]send data from one fragment to show in listview in another fragment

Scenario is: I have two fragments FragmentA and FragmentB. 场景是:我有两个片段FragmentA和FragmentB。 In FragmentA, I have a simple form and in FragmentB, I am showing data of that form in ListView. 在FragmentA中,我有一个简单的表单,在FragmentB中,我在ListView中显示了该表单的数据。

I am getting data in listview but not in real time. 我在listview中获取数据,但不是实时获取。 Like first I insert data, then, I restart Application to see data in list. 像首先插入数据一样,然后重新启动应用程序以查看列表中的数据。

My question is how to notify change from one fragment to another in real time. 我的问题是如何实时通知从一个片段到另一个片段的更改。

you can use listener in your activity and then notify:
class MyActivity extends Activity {
FragmentA fragA;
FragmentB fragB;

public void notifyB() {
    fragB.notifyB();
}

class FragmentA extends Fragment{
 private void notifyB(){
     ((MyActivity) getActivity()).notifyB();
 }
class FragmentB extends Fragment{
    public notifyB() {
         // do something
    }
}

After setting the variable of the FragmentTransaction you can do like this: 设置FragmentTransaction的变量后,您可以执行以下操作:

Bundle bundle = new Bundle();
bundle.putString("flag", variable)// You can pass Strings, Int, Boolean. 
//The "flag" is the key to get the value in the other fragment.
//The variable is wat you need to pass to the other fragment.

In you new fragment you can get your viable like this: 在您的新片段中,您可以像这样获得生存能力:

Bundle bundle = new Bundle();
String a = bundle.getString("flag");
// now you have te value in the variable a

this works in eclipse 这在日食中有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM