gpt4 book ai didi

android - 当我单击 fragment B 的发送按钮时如何将 arrayList 传递给 fragment C

转载 作者:行者123 更新时间:2023-11-30 01:51:39 25 4
gpt4 key购买 nike

当我点击放置在 fragment 中的 Send 按钮时,我想将 fragment B 的完整 listView 传递到 fragment C B.

为此我应该做些什么

这是我的B类代码:

public class B extends Fragment {

Button send;
String[] list = { "A", "B", "C", "D", "E", "F", "G",
"H", "I ", "J ", "K", "L", "M", "N", "O",
"P" };

ArrayList<String> mList;
ListView LV;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.b, container, false);

LV = (ListView) V.findViewById(R.id.listView);
send = (Button) V.findViewById(R.id.send);

mList = new ArrayList<String>();

for (int i = 0; i < list.length; i++) {

mList.add(list[i]);
}

ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity()
.getBaseContext(), android.R.layout.simple_list_item_1, mList);

LV.setAdapter(aa);

return V;
}

}

和类 C::

public class C extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.c, container, false);



return rootView;
}

}

最佳答案

您使用 bundle 在 fragment 之间传递数据。在您的情况下,只需创建一个具有 arraylist 实例变量的类,并确保该类实现可序列化。使用 bundle.putSerializable() 传递模型或序列化对象例如

in fragment B:
Bundle bundle = new Bundle();
bundle.putInt("key","value");
and do fragment.setArguments(bundle)before the fragment transaction

in Fragment C
Bundle bundle=getArguments();
int num=bundle.getInt("key")

关于android - 当我单击 fragment B 的发送按钮时如何将 arrayList 传递给 fragment C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32948361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com