简体   繁体   English

使用Android Studio将对象或数组列表从一个片段传递到另一个片段

[英]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.

相关问题 (mainActifity)fragment --> 使用接口从列表中传递 object 的新片段 - (mainActifity)fragment --> new fragment passing object from list using an interface 从一个片段传递到另一个 - Passing from one fragment to another 通过单击一个片段中的按钮并更改android studio中的底部导航图标,从一个片段重定向到另一个片段 - redirection from one fragment to another by clicking a button in one fragment together with changing the bottom navigation icon in android studio 将数据从一个片段传递到另一个片段,从 FirebaseRecyclerAdapter - Passing data from one fragment to another fragment from FirebaseRecyclerAdapter Java将数据从一个片段的Lis​​tView传递到另一个片段 - Java Passing Data From One Fragment's ListView To Another Fragment 将复选框输入从一个片段传递到同一活动中的另一个片段 - Passing checkbox input from one fragment to another fragment in the same activity 将arraylist从一个活动传递到另一个活动的片段 - Passing arraylist from one activity to fragment of another 将Spinner值从一个片段传递到另一个片段 - Passing a Spinner value from one fragment to another 如何将带有其他对象数组的对象从一个片段传递到另一个片段? - How to pass an object with an array of other objects from one fragment to another? 在Android中将数据从一个片段传递到下一个片段 - Passing data from one fragment to next fragment in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM