0

I am trying to pass a data from a clickable TextView to EditText between the fragments. Right now I am able to navigate to the other fragment when clicking the TextView. This is the coding of CategoryFragment:

public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        getActivity().getApplicationContext(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                listDataHeader.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT)
                        .show();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                HomeFragment fragment = new HomeFragment();
                fragmentTransaction.replace(R.id.frame_container, fragment);
                fragmentTransaction.commit();
                return false;
            }

I feel that this can be done because the toast is able to retrieve the string inside the listDataChild. What I can't seem to solve overnight is that how to retrieve the listDataChild strings and pass it, from Fragment 1 to Fragment 2, where Fragment 2 contains one EditText box. How can I put the string from textview to edittext?

Saket Mittal
  • 3,726
  • 3
  • 29
  • 49
  • 3
    This has been answered countless times before on StackOverflow. http://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments – Bidhan Jun 06 '15 at 04:04

2 Answers2

1

You can pass data between fragments using bundle...

Bundle args = new Bundle();

HomeFragment fragment = new HomeFragment();

args.putInt("key", value);

fragment .setArguments(args);
0

In CategoryFragment pass data using Bundle like

HomeFragment fragment = new HomeFragment();
Bundle bundle = new Bundle();
bundle.putString("key", listDataChild.get(
                                listDataHeader.get(groupPosition)).get(
                                childPosition));
fragment.setArguments(bundle);

In HomeFragment use this code in onActivityCreated(....)

Bundle bumdle = getArguments();
if (bundle  != null)
 String value = bundle.getString("key");
Avinash Kumar Pankaj
  • 1,700
  • 2
  • 18
  • 27
  • 1
    Passing values to Fragment using a constructor is a bad practice. You should use Bundles instead. – Bidhan Jun 06 '15 at 04:09