使用 bundle 在片段之间传递数据到另一个片段示例 [英] using bundle to pass data between fragment to another fragment example
问题描述
我的应用中有 3 个 sherlockListFragment.每个片段都有一些编辑文本,最后一个片段有一个按钮,当按下该按钮时,应该访问和存储第一个和第二个片段中的所有数据.我使用 bundle 在片段之间发送数据.使用以下简单示例,这是我的第一个片段的代码:
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);
}
}这是接收文本的片段的代码:
} 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
推荐答案
您的代码失败是正常的.在第一个片段中,您所做的就是创建一个 PersonalDataFragment
的新实例,然后将数据传递给它一个 Bundle
.问题是尽管 fragment 将数据保存在 Bundle
中,但片段本身并不是应用程序使用的片段(甚至没有附加到 Activity代码>).您还在
PersonalDataFragment
实例上设置了 Bundle
,但您尝试访问 WorkExpRefFragment
中的数据,这显然不起作用,因为这两个片段不没有直接联系.
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.
您想要做的一个简单的解决方案是让 Activity
保存"片段的数据,因为 Activity
可用于所有片段.首先在持有三个片段的Activity
中创建两个方法:
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);
}
}
要检索 WorkExpRefFragment
片段中的数据:
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();
}
根据您使用这些片段的方式,此解决方案可能不起作用.另外,请记住,您像上面一样传递的 Bundle
不会因配置更改而保留.
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.
这篇关于使用 bundle 在片段之间传递数据到另一个片段示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!