使用Bundle在Fragment之间传递数据的例子

6
我在我的应用中有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);
}

现在第三个片段中有一个空值,我该怎么办?提前感谢。
2个回答

6
在编写代码时出现错误是很正常的。在第一个片段中,您只需创建一个新的PersonalDataFragment实例,并向其中传递数据Bundle。问题在于,虽然片段Bundle中保存着数据,但该片段本身并不是应用程序使用的片段(甚至未连接到Activity)。您还将Bundle设置在了一个PersonalDataFragment实例上,但尝试在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不会在配置更改时被保留。


谢谢您的回复,这对我有用,但我需要在第三个片段中的按钮按下时获取数据,而不是在创建第一个片段时获取。我希望它能够保留配置更改。 那么,我该怎么做? - Fareed
我懂了,并且添加了addTextChanged监听器 :) - Fareed

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接