0

我是 Android 新手,有一个问题:

我想将最喜欢的 Arraylist 从 FavListFragment 转移到 ApplistFragment。有人可以告诉我该怎么做吗?我尝试使用 Bundle 和 putStringArrayList 传输数据,但这不起作用。我希望有人可以帮助我,我现在正在寻找几个小时。

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");  
4

1 回答 1

4

好的,您可以通过关联的活动来做到这一点,看这个例子:

与其他片段通信

问候。

于 2013-05-20T23:06:37.680 回答