当我点击片段B的发送按钮时,如何将arrayList传递给片段C.

时间:2015-10-05 12:13:53

标签: android

当我点击listView B中的发送按钮时,我想将fragment B的完整fragment传递到fragment C。

为此我应该做什么

这是我的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;
}

}

1 个答案:

答案 0 :(得分:1)

您使用bundle来在片段之间传递数据。在您的情况下,只需创建一个具有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")