简体   繁体   English

如何将Arraylist从一个片段传输到另一个片段

[英]How to transfer Arraylist from one fragment to another

I'm new to Android and have a question: 我是Android新手,但有一个问题:

I want to tranfer the the fav Arraylist from the FavListFragment to the ApplistFragment. 我想将收藏的Arraylist从FavListFragment转移到ApplistFragment。 Can someone tell me how to do this? 有人可以告诉我该怎么做吗? I tried to tranfser the data with Bundle and putStringArrayList but this didn't work. 我试图使用Bundle和putStringArrayList传输数据,但这没有用。 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");  

Ok, you can do that through the associated Activity, watch this example: 好的,您可以通过关联的“活动”执行此操作,请观看以下示例:

Comunnicating with other fragments 与其他片段沟通

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将价值从一个片段转移到另一个片段 - How to transfer value from one fragment to another 如何在 Android 中从一个片段转移到另一个片段 - How to transfer from one fragment to another in Android 如何将数据从一个 Fragment 传输到另一个 Fragment? - How to transfer data from one Fragment to another? 如何将价值从一个片段转移到另一个片段 - How to transfer value from one fragment to another fragment 将数据从一个片段传输到另一个片段 - Transfer data from one fragment to another fragment 如何将 ArrayList 个对象从一个 Activity 转移到另一个 Activity - How to transfer an ArrayList of objects from one Activity to another 如何通过活动将额外数据从一个片段传输到另一个片段 - how to transfer extra data from one fragment to another through activity 如何将值传输到从另一个片段接收到的值的recylerview? - How to transfer the value to recylerview of one which is received from another fragment? 如何发送ArrayList <NameValuePair> 从一个片段到另一个在android - How to send ArrayList<NameValuePair> from one fragment to another in android 将arrayList从一个片段传递到另一个 - Pass arrayList from one fragment to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM