使用束片段之间的数据传递到另一个片段的例子片段、例子、数据

2023-09-04 07:10:52 作者:阿弟

我有3 sherlockListFragments在我的应用程序。每个片段具有一些editTexts和最后片段具有一个按钮,当它的pssed在第一和第二片段中的所有数据应该被访问和存储$ P $。 我用束片段之间发送数据。用简单的以下示例中, 这是我的第一个片段的code:

I have 3 sherlockListFragments in my app. Every fragment has some editTexts and the last fragment has a button which when it's pressed all data in the first and second fragments should be accessed and stored. I used bundle to send data between fragments. with the simple following example, This is the code of my first fragment :

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

} 这是接收文本片段的code:

} and this is the code of the fragment that receives the text :

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

}

现在我在第三个片段的空,我应该怎么办? 在此先感谢

Now I have a null in the third fragment, what should i do ? Thanks in advance

推荐答案

这是正常的,您的code失败。在第一个片段你要做的就是创建 PersonalDataFragment 的新实例,你的数据传递给它一个捆绑。现在的问题是,虽然片段的保持在捆绑,片段本身是不使用的应用程序中的一(甚至不附加到数据在活动)。您还可以设置捆绑 PersonalDataFragment 实例,但您尝试访问数据的 WorkEx prefFragment 这将明显不一样的两个片段工作没有直接联系。

It's normal that your code fails. In the first fragment all you do is create a new instance of PersonalDataFragment and you pass it a Bundle with the data. The problem is although fragment holds the data in the Bundle, the fragment itself is not the one used by the app(isn't even attached to the Activity). You also set the Bundle on a PersonalDataFragment instance but you try to access the data in a WorkExpRefFragment which will obvious not work as the two fragments don't have a direct connection.

一个只为你想要做什么解决方法是让活动拯救你的片段作为活动数据适用于所有的碎片。首先,创建于活动拿着三个片段两种方法:

One simply solution for what you want to do is to let the Activity "save" the data for your fragments as the Activity is available for all fragments. First create two methods in the Activity holding the three fragments:

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
} 

那么你的碎片将节省他们的数据是这样的:

Then your fragments will save their data like this:

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

要检索的 WorkEx prefFragment 片段中的数据:

To retrieve the data in the WorkExpRefFragment fragment:

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

根据您使用的这些碎片这种解决方案可能无法正常工作。另外,请记住,捆绑你通过像上面将不会保留更改配置。

Depending on how you used those fragments this solution might not work. Also, keep in mind that the Bundle you pass like above will not be retained for a configuration change.