-2

I have two Fragment, I want to send the list view data from fragment 1 to fragment two.

when the user clicks the button on Fragment1_listview.xml the fragment2_listview will appear and it has the same data from fragment1.

MainActivity.java

           @Override
           protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_cart);

        CartFragment();
               FragmentManager manager = getSupportFragmentManager();
               final int commit = 
        manager.beginTransaction().replace(R.id.fragment_container, cartFragment,
                       cartFragment.getTag()).commit();

           }

           }

Fragment1.java

        @Override
        public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                                Bundle savedInstanceState) {
           view = inflater.inflate(R.layout.fragment_cart, container, false);




           return view;
        }

            @Override
            public void processFinish(String s) {



           try {
               itemList = new JsonConverter<Cart>().toArrayList(s, Cart.class);
           } catch (Exception e) {
               e.printStackTrace();
           }


           BindDictionary dic = new BindDictionary();

                   dic.addStringField(R.id.tvName, new StringExtractor<Cart>() {
               @Override
               public String getStringValue(Cart item, int position) {
                   return item.orderName;
               }
           });

           dic.addStringField(R.id.tvDesc, new StringExtractor<Cart>() {
               @Override
               public String getStringValue(Cart item, int position) {
                   return item.description;
               }
           }).visibilityIfNull(View.GONE);



           adapter = new FunDapter<>(CartFragment.this.getActivity(), itemList, 
           R.layout.cart_row, dic);
           lv = (ListView)view.findViewById(R.id.lvCart);
           lv.setAdapter(adapter);

           lv.setOnItemClickListener(this);


    }

Fragment2.java

        public View onCreateView(LayoutInflater inflater, final  ViewGroup 
        container,
                                Bundle savedInstanceState) {

            view = inflater.inflate(R.layout.fragment_payment, container, false);


       return view;
    }

but I don't know how to do it. please help me.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Kny
  • 95
  • 8
  • 1
    Possible duplicate of [How to pass values between Fragments](https://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments) – Santanu Sur Mar 14 '18 at 06:57
  • sir @SantanuSur i have a fragment1_row which is holder of the data that fetch from database – Kny Mar 14 '18 at 07:01

1 Answers1

0

I think the best solution for you is to have the variables inside the main activity and access them from fragments. I mean, if you have to do THE SAME in all fragments, you can write the code inside the activity and just call the method you need.

SahdevRajput74
  • 754
  • 7
  • 18