0

I have this code, which gets data from various EditTexts and adds this info into a ListView_item in fragment 1. This ListView_item is supposed to be populated in the ListView in fragment 2. I was thinking this might be possible with the help of the EventBus library but I have not quite yet gotten my head around how.

Here is my populate list method

public void populateList() {
    ArrayAdapter<Appointment> adapter = new AppointmentListAdapter();
    (The listview in fragment 2).setAdapter(adapter);
}
ndsmyter
  • 6,535
  • 3
  • 22
  • 37
Harrimf
  • 29
  • 1
  • 1
  • 9

1 Answers1

0

You have two ways to do this :

1) Using Interface: You will find lots of links for how to use interface for data passing.

2) Pass adapter through the activity :

step 1 : In your Fragment1 Make getParent() method :

private MyActivity getParent() {
        return ((MyActivity) getActivity());
}

step 2 : In MyActivity Make populateList() method :

public void populateList(ArrayAdapter<Appointment> adapter){
        SecondFragment mSecondFragment = (SecondFragment) getFragmentManager().findFragmentByTag(SecondFragment.TAG);
        mSecondFragment.setAdapter(adapter);
}

step 3 : In your Fragment1 populateList method look like below :

public void populateList() {
        ArrayAdapter<Appointment> adapter = new AppointmentListAdapter();
        getParent().populateList(adapter);
}
Jaydeep Patel
  • 146
  • 2
  • 9