如何将Arraylist从一个片段转移到另一个片段

时间:2013-05-20 22:32:25

标签: android android-fragments bundle android-listfragment

我是Android新手并且有一个问题:

我想将fav Arraylist从FavListFragment转移到ApplistFragment。 谁能告诉我怎么做?我尝试使用Bundle和putStringArrayList对数据进行tranfser,但这不起作用。我希望有人可以帮助我,我现在正在寻找几个小时。

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;
    }

她的另一个片段

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;


}

这是我试过的:

AppListFragment fr = new AppListFragment();
                        Bundle value= new Bundle();
                        value.putStringArrayList("temp1", fav);
                        fr.setArguments(value);

Bundle bundle=getArguments(); 
    ArrayList <String> myStrings = bundle.getStringArrayList("temp1");  

1 个答案:

答案 0 :(得分:4)

好的,你可以通过相关的Activity做到这一点,看看这个例子:

Comunnicating with other fragments

问候。