1

I want to pass complete listView of fragment B into fragment C when i click on Send Button which is placed in fragment B.

for this what i should have to do

here is my code of class B:

public class B extends Fragment {

Button send;
String[] list = { "A", "B", "C", "D", "E", "F", "G",
        "H", "I ", "J ", "K", "L", "M", "N", "O",
        "P" };

ArrayList<String> mList;
ListView LV;

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

    LV = (ListView) V.findViewById(R.id.listView);
    send = (Button) V.findViewById(R.id.send);

    mList = new ArrayList<String>();

    for (int i = 0; i < list.length; i++) {

        mList.add(list[i]);
    }

    ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity()
            .getBaseContext(), android.R.layout.simple_list_item_1, mList);

    LV.setAdapter(aa);

    return V;
}

}

and class C::

public class C extends Fragment {

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

    View rootView = inflater.inflate(R.layout.c, container, false);



    return rootView;
}

}
Subramanyam M
  • 335
  • 2
  • 6
  • 18
  • So? What is your approach ? It would be great if you show your findings – Shahzeb Oct 05 '15 at 12:18
  • 1
    @Ядм Жцмдшдт : Another option is to make arraylist as a activity level variable and use on every fragment loaded in that activity..!! – AndiGeeky Oct 05 '15 at 12:22

1 Answers1

1

you use bundle for data passing between fragments. In your case just create a class that has a arraylist instance variable and make sure the class implements serializable. use bundle.putSerializable() to pass models or serializable objects For ex

in fragment B:
Bundle bundle = new Bundle();
bundle.putInt("key","value");
and do fragment.setArguments(bundle)before the fragment transaction

in Fragment C
Bundle bundle=getArguments();
int num=bundle.getInt("key")
Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
  • You r ri8, But How the whole ArrayList will pass from Fragment B to Fragment C using Bundle ? – Jaimin Modi Oct 05 '15 at 12:18
  • Its Ok to pass a keyValue. But, How to Pass the Whole ArrayList ? – Jaimin Modi Oct 05 '15 at 12:22
  • 1
    http://stackoverflow.com/questions/15324741/bundle-arraylist-arraylistinteger refer this – Aniruddha K.M Oct 05 '15 at 12:25
  • In the given code it's not clear what is the "fragment" variable in fragment B. It must be a reference to the fragment C of course, but how does fragment B get it? Also exactly what kind of "fragment transaction" should one do in fragment B to modify the bundle of an already existing(?) fragment C? Clarifying these would improve the answer. Also [the documentation](https://developer.android.com/training/basics/fragments/communicating.html) says that "All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly". – Markus Kauppinen Oct 06 '15 at 08:58