[英]Passing object or Array List from one Fragment to another Fragment using Android Studio
I am trying to pass the object or the arrayList from this fragment to another. 我试图将对象或arrayList从此片段传递到另一个。 I was using the Intent method but I had the same problem which I do not know what parameter should I put in bundle.putParcelable() method.
我使用的是Intent方法,但遇到了同样的问题,我不知道应该在bundle.putParcelable()方法中放入什么参数。
BlankFragment1 sendData = new BlankFragment1();
final Series s = new Series(itemName.getText().toString(),
Integer.parseInt(itemYear.getText().toString())
,Integer.parseInt(itemNumberofSeas.getText().toString()),
dropDown.getSelectedItem().toString());
Bundle bundle = new Bundle();
bundle.putParcelable(s);
sendData.setArguments(bundle);
You can see below example 你可以看下面的例子
Fragment one 片段一
android.support.v4.app.FragmentTransaction ft =
getActivity().getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
OfficeCategoryFragment frag = new OfficeCategoryFragment();
Bundle bundles = new Bundle();
Division aDivision = divisionList.get(position);
// ensure your object has not null
if (aDivision != null) {
bundles.putSerializable("aDivision", aDivision);
Log.e("aDivision", "is valid");
} else {
Log.e("aDivision", "is null");
}
frag.setArguments(bundles);
ft.replace(android.R.id.content, frag);
ft.addToBackStack(null);
ft.commit();
fragment 2 片段2
Bundle bundle = getArguments();
Division division= (Division) bundle.getSerializable("aDivision");
Log.e("division TEST", "" + division.getName());
Use setArguments to add the data same as putextra in activity. 使用setArguments在活动中添加与putextra相同的数据 。
Bundle bundle = new Bundle();
bundle.putString("latitude", latitude);
bundles.putSerializable("KEY_ARRAYLIST", DIVID);
bundle.putString("longitude", longitude);
bundle.putString("board_id", board_id);
MapFragment mapFragment = new MapFragment();
mapFragment.setArguments(bundle);
And to get data use getArguments same as getExtra in Activity 为了获得数据,请使用与Activity中的getExtra相同的getArguments
String latitude = getArguments().getString("latitude")
Arraylist obj= getArguments().getSerializable("KEY_ARRAYLIST");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.