[英]How to transfer Arraylist from one fragment to another
I'm new to Android and have a question: 我是Android新手,但有一个问题:
I want to tranfer the the fav Arraylist from the FavListFragment to the ApplistFragment. 我想将收藏的Arraylist从FavListFragment转移到ApplistFragment。 Can someone tell me how to do this? 有人可以告诉我该怎么做吗? I tried to tranfser the data with Bundle and putStringArrayList but this didn't work. 我试图使用Bundle和putStringArrayList传输数据,但这没有用。 I hope someone can help me with this, i'm searching for hours now. 我希望有人可以帮助我,我现在正在寻找几个小时。
public class FavListFragment extends ListFragment {
ArrayList<String> list;
ArrayList<String> fav = new ArrayList<String>();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_dummy, container, false);
Button button = (Button) view.findViewById(R.id.button1);
Button button2 = (Button) view.findViewById(R.id.button2);
for(int i = 0; i < 5; i++)
list.add("item" + i);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_multiple_choice, list);
setListAdapter(adapter2);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
});
button2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
int cntChoice = getListView().getCount();
SparseBooleanArray sparseBooleanArray = getListView()
.getCheckedItemPositions();
for (int i = 0; i < cntChoice; i++) {
if (sparseBooleanArray.get(i)) {
String selected = "" +
getListView().getItemAtPosition(i).toString();
System.out.println(selected);
fav.add(selected);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
}
}
});
return view;
}
And her the other fragment 而她的另一个片段
public class AppListFragment extends ListFragment {
ArrayList<String> list;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_dummy, container, false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list );
setListAdapter(adapter);
return view;
}
This ist what I tried: 这是我尝试的:
AppListFragment fr = new AppListFragment();
Bundle value= new Bundle();
value.putStringArrayList("temp1", fav);
fr.setArguments(value);
Bundle bundle=getArguments();
ArrayList <String> myStrings = bundle.getStringArrayList("temp1");
Ok, you can do that through the associated Activity, watch this example: 好的,您可以通过关联的“活动”执行此操作,请观看以下示例:
Comunnicating with other fragments 与其他片段沟通
Regards. 问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.