【问题标题】:using bundle to pass data between fragment to another fragment example使用 bundle 在片段之间传递数据到另一个片段示例
【发布时间】:2013-05-11 16:31:42
【问题描述】:

我的应用中有 3 个 sherlockListFragments。每个片段都有一些editTexts,最后一个片段有一个按钮,当它被按下时,第一个和第二个片段中的所有数据都应该被访问和存储。 我使用 bundle 在片段之间发送数据。通过以下简单示例, 这是我的第一个片段的代码:

public class PersonalDataFragment extends SherlockListFragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragmet_personal_data, container, false);

    return v;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    PersonalDataFragment fragment = new PersonalDataFragment();
    Bundle bundle = new Bundle();
    bundle.putString("y", "koko"); //any string to be sent
    fragment.setArguments(bundle);

    super.onCreate(savedInstanceState);
}

} 这是接收文本的片段的代码:

public class WorkExpRefFragment extends SherlockListFragment  {
String myInt;

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_workexp_ref, container, false);


    final EditText t = (EditText) view.findViewById(R.id.editText5);
    final Button generate = (Button)view.findViewById(R.id.button2);

    generate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            t.setText(myInt + "sadfigha");
        }
    });
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    Bundle bundle = this.getArguments();
    if(getArguments()!=null) {
        myInt = getArguments().getString("y");
    }

    super.onCreate(savedInstanceState);
}

}

现在我在第三个片段中有一个空值,我该怎么办? 提前致谢

【问题讨论】:

    标签: android android-fragments android-listfragment


    【解决方案1】:

    您的代码失败是正常的。在第一个片段中,您所做的只是创建一个 PersonalDataFragment 的新实例,然后将 Bundle 与数据一起传递给它。问题是虽然 fragment 将数据保存在 Bundle 中,但该片段本身并不是应用程序使用的片段(甚至没有附加到 Activity)。您还在PersonalDataFragment 实例上设置了Bundle,但您尝试访问WorkExpRefFragment 中的数据,这显然不起作用,因为这两个片段没有直接连接。

    您想要做的一个简单的解决方案是让Activity“保存”您的片段的数据,因为Activity 可用于所有片段。首先在Activity中创建两个方法,分别持有三个片段:

    public void saveData(int id, Bundle data) {
        // based on the id you'll know which fragment is trying to save data(see below)
        // the Bundle will hold the data
    }
    
    public Bundle getSavedData() {
        // here you'll save the data previously retrieved from the fragments and
        // return it in a Bundle
    } 
    

    然后你的片段将像这样保存它们的数据:

    public class PersonalDataFragment extends SherlockListFragment {
    
       // this will identify the PersonalDataFragment fragment when trying to save data 
       public void static int id PERSONAL_ID = 1; 
       //...
    
       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Bundle bundle = new Bundle();
          bundle.putString("y", "koko"); //any string to be sent
          YourActivity activity = (YourActivity) getActivity();
          activity.saveData(PERSONAL_ID, bundle);
       }    
    }
    

    要检索WorkExpRefFragment 片段中的数据:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        YourActivity activity = (YourActivity) getActivity();
        Bundle savedData = activity.getSavedData();
    }
    

    根据您使用这些片段的方式,此解决方案可能不起作用。另外,请记住,您在上面传递的Bundle 不会保留用于配置更改。

    【讨论】:

    • 感谢您的回复,它对我有用,但是当按钮(在第三个片段中)按下时,我需要它来获取数据而不是第一个片段。被建造。我希望保留它以进行配置更改。那么,我该怎么办?
    • 我得到它并制作了 addTextChanged 监听器 :) 谢谢
    猜你喜欢
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多