Search code examples
androidandroid-fragmentsandroid-listfragment

"Refreshing" a fragment from the MainActivity


So, there is a button in a ListFragment. The onCLick method of the button is implemented in the MainActivity(not sure if it is a proper solution, but it is what it is). When I click the button the AlertDialog pops up and when I choose one of the dialog options it changes the dataset my fragment is working with.

The problem is when the AlertDialog disappears, my ListFragment is still displaying old data.

Is there any way to update my ListFragment from the MainActivity?

I've tried making certain ListFragment methods static so that they could be called from the main activity, but those methods use non-static fields, etc. and thus cannot be static.


Solution

  • So, I declared an adapter of my ListFragment fragment as static, as well as the I declared a list from which this adapter is being filled - as static.

    From the main activity I do this:

    ListFragment.item.add(mChosenFilePath);
    ListFragment.fileList.notifyDataSetChanged();
    

    where:

    item - is a list that contains the elements that are to be displayed

    mChosenFilePath - path of file that has been added into the item as a result of the dialog

    fileList - is my adapter