将数据从活动传递到片段transaction.replace() [英] Passing data from activity to fragment transaction.replace()

查看:41
本文介绍了将数据从活动传递到片段transaction.replace()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将数据从活动传递到片段时我遇到问题(片段重叠):我需要将带有包含复选框ID的整数的arraylist传递给被选中...

I have a problem when passing data from activity to fragment(fragments are overlapped): I need to pass arraylist with integer that contains id of checkboxes to checked...

在传递数据之前:在此处输入图片描述

通过数据后:在此处输入图片描述

将数据从活动传递到片段的代码:

Code to pass data from activity to fragment:

 Bundle bundle = new Bundle();
    bundle.putIntegerArrayList("oki", hm);
    System.out.println("PERO:" + bundle);

    MyListFragment myFragment = new MyListFragment();
    myFragment.setArguments(bundle);

   FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.container, myFragment);


    transaction.commit();

获取数据的代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the custom_spinner_items for this fragment
        //super.onDestroy();
        try {
        bundle = getArguments();

        System.out.println("BUNDLES:" + bundle);
        //if (bundle != null) {


                strtext = bundle.getIntegerArrayList("oki");
                System.out.println("AH:" + strtext);


        //}
        }catch(Exception e){

        }

推荐答案

在您的活动中

     private List<Data> List = new ArrayList<>();        

     FragmentName FragName = new FragmentName();
     Bundle bundle = new Bundle();                              
     bundle.putParcelable("key",List);
     FragName.setArguments(addEditBundle);
    
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
     transaction.replace(R.id.container, FragName);
    
     transaction.commit();

在您的片段中

     private List<Data> List = new ArrayList<>();  
     List  = getArguments().getParcelable("Key");

这篇关于将数据从活动传递到片段transaction.replace()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆