user2127925
user2127925

Reputation: 67

How to transfer Arraylist from one fragment to another

I'm new to Android and have a question:

I want to tranfer the the fav Arraylist from the FavListFragment to the ApplistFragment. Can someone tell me how to do this? I tried to tranfser the data with Bundle and putStringArrayList but this didn't work. 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");  

Upvotes: 0

Views: 8577

Answers (1)

Remy Ticona
Remy Ticona

Reputation: 343

Ok, you can do that through the associated Activity, watch this example:

Comunnicating with other fragments

Regards.

Upvotes: 4

Related Questions