How to update the ListView of a Fragment from another Fragment?
Asked Answered
K

3

6

How can I communicate a listview of a ListFragment after an event of a Fragment inside another Fragment?

In the ListFragment (fragment A) I have a method to refresh the ListView. But also, I need to refresh it after a click of a Button inside a Fragment, wich is child of another Fragment (fragment b)

Its like Fragment A (listFragment) | Fragment B (detailview) (fragment C - child fragment of B)

How can I do it?

Keloid answered 13/3, 2014 at 17:18 Comment(2)
in my case, i store the reference to List Fragment in a global variable and use it every where, just make sure the variable is updated with new reference to listView when ever the list was recreated.Kingston
The best way to do this is to use the containing Activity to handle the communication. First Create an interface in your Fragment, implement it in your Activity, and from your Activity find the Fragment you want to share the information with. Then call the method using the fragment's reference to update the list.Crocket
J
8

You can access another Fragment by its tag:

 // find your fragment
 YourFragment f = (YourFragment) getSupportFragmentManager().findFragmentByTag("yourFragTag");

 // update the list view
 f.updateListView(); 

The tag of your Fragment is set when it is attached to a container layout:

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frameBuy, YourFragment.newInstance(), "yourFragTag").commit();

So when you click your Button, find the Fragment you want to refresh by its tag, and then call your refresh method.

IF you are using a ViewPager, this is how to get the Fragments tag:

   /**
     * Gets the fragment tag of a fragment at a specific position in the viewpager.
     *
     * @param pos the pos
     * @return the fragment tag
     */
    public String getFragmentTag(int pos){
        return "android:switcher:"+R.id.yourViewPagerId+":"+pos;
    }
Jesuit answered 13/3, 2014 at 17:21 Comment(0)
E
0

You can do it with a few simple steps:

  1. Create a listener interface for component to listen to the button click event from FragmentC. For example:

    public interface FragmentCButtonListener { public void onButtonClicked(); }

  2. Add a method in FragmentC to allow listener registration. FragmentC will keep track of the listener, and call the listener callback as soon as the button is clicked. For example:

    public class FragmentC extends Fragment { FragmentCButtonListener myListener; public void registerListener (FragmentCButtonListener listener) { myListener = listener; } }

  3. FragmentA should implement FragmentCButtonListener interface, register itself as a listener to FragmentC, and refresh the list view when it receives the callback from FragmentC. For example:

    public class FragmentC extends Fragment implements FragementCButtonListener { FragmentC fragmentC; public void onCreate() { fragment = new FragmentC(); fragment.registerListener (this); }

    public void onButtonClicked() {
        //refresh the list view
    }
    

    }

Please note, I assume the FragmentA has a reference to FragmentC in the sample. If not, just make sure the container class of all fragments registers itself as the listener of FragmentC. The container class can as FragmentA to update listview once it receives callback from FragmentC.

Endearment answered 13/3, 2014 at 18:9 Comment(0)
C
0

follow these steps We have two fragments called AddFragmentand ListFragment, and upon adding an item on first fragment you want the updated list be shown on list fragment (what sort of sorcery is this!!!).

Step 1 create the listener interface on class level of AddFragment with a method that is going to be implemented by the other guy (ListFragment ) and create Interface type variable

public class AddFragment extends Fragment{
 //listener varriable

 //listener
  public interface OnCategoryAddedListener{
    public void refreshList();
}
 private static OnCategoryAddedListener meAddListener;
}

Step 2 create register method on class level of the same AddFragment class and set listenter variable

public class AddFragment extends Fragment{


public void registerListener(OnCategoryAddedListener listener)
{
    meAddListener = listener;
}
}

Step 3 upon any event cud be button click or yelling at ur application(that is considered rude event in android :-) ) check for listener object meAddListener variable and call the interface, in a Shakespeare’s nutshell it means “for thou who implement ye interface and brought the method within ur class shelter, I shall give u my utmost privilege and blessing to …. ”

Step 4 On ListFragment implement the AddFragment’s interface,no turning back just go implement its method. Within that method u just call abracadabra to repopulate ur list or any sort of updatable android view object… and ur done

public class ListFragment extends Fragment implements AddFragment.OnCattegoryAddedListener{
//refer to AddFragment
AddFragment addFragment;

//once the fragment is within the memory scope u instantiate AddFragment
//and register listener with AddFragment context which inherently implement OnCategoryAddedListener

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        categoryAddFragment = new CategoryAddFragment();
        categoryAddFragment.registerListener(this);

    }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    fillList();

}

public void fillList() {

    ArrayAdapter<String>   catArrayAdapter = new ArrayAdapter<>(context,android.R.layout.simple_list_item_1,getItems());
    setListAdapter(catArrayAdapter);

}
//le interface method u can count on to refresh ur list content
public void refreshList(){
fillList();
}

check this out for a little more enjoy the java magic

Cruel answered 4/3, 2016 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.