0

I have a Page Viewer with three pages with ArrayLists in them, represented as "Tomorrow" "Today" and "Yesterday". I also have a Drawer that allows to change settings of the Lists.

When the Drawer closes, I want the ListsViews (or the entire page fragment) to update to show three new ArrayLists that were created after the new settings were applied.

So far, I managed to be able to update "Yesterday" and "Tomorrow" (When sliding to yesterday, tomorrow updates and vice versa), I think that is because "Today" never gets destroyed.

Either way, I would really like to see all three update as soon as the Drawer closes.

Here is the code for my Adapter:`

    private class MyPagerAdapter extends FragmentPagerAdapter {

    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
    ListFragmentOfTomorrow torrowFragment =new ListFragmentOfTomorrow();
    ListFragmentOfToday todayFragment = new ListFragmentOfToday();
    ListFragmentOfYesterday yestFragment = new ListFragmentOfYesterday();

    public MyPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int pos)
    {
     switch (pos) {

        case 0:
            torrowFragment.newInstance(tomorrowArrayList, MainActivity.this);
            registeredFragments.put(0, torrowFragment);
            return torrowFragment;
        case 1:
            todayFragment.newInstance(todayArrayList, MainActivity.this);
            registeredFragments.put(1, todayFragment);
            return todayFragment;
        case 2:
            yestFragment.newInstance(yesterdayArrayList, MainActivity.this);
            registeredFragments.put(2, yestFragment);
            return yestFragment;
        default: 
            todayFragment.newInstance(todayArrayList, MainActivity.this);
            registeredFragments.put(3, todayFragment);
            return todayFragment;
            }
    }

    @Override
    public int getCount() {
        return 3;
    }

    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }
}`

This is the code for one of the page Fragment (all three are basically the same):

public class ListFragmentOfToday extends Fragment
{
static ExpandListAdapter ExpAdapter;
static ExpandableListView expndList;
static Context context;
static ArrayList<Game> todayArrayList;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState)
{
    View v = inflater.inflate(R.layout.fragment_list_of_today, container,  false);
    expndList = new ExpandableListView(context);
    expndList = (ExpandableListView)v.findViewById(R.id.FragmentedExpandableListView);
    ExpAdapter = new ExpandListAdapter(context,todayArrayList);
    expndList.setAdapter(ExpAdapter);
    return v;
}

public static ListFragmentOfToday newInstance(ArrayList<Game> todayArrayListIn, Context contextIn)
{
    ListFragmentOfToday todayFragment = new ListFragmentOfToday();
    context = contextIn;
    todayArrayList = todayArrayListIn;
    return todayFragment;
}

public void RefreshList(ArrayList<Game> todayArrayListIn)
{
    todayArrayList=todayArrayListIn;
    ExpAdapter.notifyDataSetChanged();
}
}

This is the code for when the Drawer closes:

    @Override
public void onDrawerClosed(View drawerView)
{
    //Here, the new ArrayLists are created(...)
    ListFragmentOfTomorrow tomorrowFragmentToUpdate = (ListFragmentOfTomorrow)pagerAdapter.getRegisteredFragment(0);
    tomorrowFragmentToUpdate.RefreshList(updatedTomorrowArrayList);
    ListFragmentOfToday todayFragmentToUpdate = (ListFragmentOfToday)pagerAdapter.getRegisteredFragment(1);
    todayFragmentToUpdate.RefreshList(updatedTodayArrayList);
    ListFragmentOfYesterday yesterdayFragmentToUpdate = (ListFragmentOfYesterday)pagerAdapter.getRegisteredFragment(2);
    yesterdayFragmentToUpdate.RefreshList(updatedYesterdayArrayList);
}

Question: How can I get all three pages to show the new updated arraylists as soon as the drawer closes?

As a new developer and a new StackOverflow user, I would also like to get any feedback on my code writing and my question format. Thank you.

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
Nakab
  • 55
  • 10
  • 1
    Call notifyDataSetChanged() on the adapter. And maybe you need to override getItemPosition() like this: @Override public int getItemPosition(Object object) { return POSITION_NONE; } – Thomas R. Jun 02 '15 at 10:51
  • Also note [instantiating fragments](http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment). One day I'll find out who came out with all this `newInstance()` stuff and ask him/her if he/she thought this through. – dhke Jun 02 '15 at 10:58

1 Answers1

0

Thank you Thomas! That worked. I tried notifyDataSetChanged() and getItemPosition(Object object), but never together. dhke - I saw the instantiating fragments when I was looking for an answer, but understood the difference to newInstance()...

Nakab
  • 55
  • 10