Search code examples
androidandroid-fragmentsandroid-viewpagerandroid-tablayoutandroid-tabs

Update an Adapter inside a Fragment part of a ViewPager after a button click on main activity?


I'm updating my app layout to use Fragments, using sliding tabs in a viewpager. I have a button on my Main Activity that wipes a database and I want that button to also "refresh" the adapter in the fragment. Previously without fragments I just used adapter.notifyDatasetHasChanged() and it worked fine. Problem is now I don't know how to tell the fragment that the button in the Main Activity has been clicked.

I looked into using an Interface but I can't apply a method on the fragment (fragment.setListener..) since it's using a view pager and I can't retrieve it.

Fragments implementation into MainActivity :

    ViewPager viewPager = findViewById(R.id.viewpager);
    SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(this, getSupportFragmentManager());

    viewPager.setAdapter(adapter);

    TabLayout tabLayout = findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

In the Fragment:

    final AppDatabase db = Room.databaseBuilder(getContext(), AppDatabase.class, "database")
            .allowMainThreadQueries().build();


    taskList = db.TaskDao().getAllTasks();

    final TaskAdapter taskAdapter = new TaskAdapter(getActivity(), taskList);

    listView.setAdapter(taskAdapter);

I want to be able to notify this adapter (taskAdapter) that the dataset has changed when i press a button in the MainActivity.

SimpleFragmentPagerAdapter :

public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {

/** Context of the app */
private Context mContext;

public SimpleFragmentPagerAdapter(Context context, FragmentManager fragmentManager) {
    super(fragmentManager);
    mContext = context;
}

@Override
public Fragment getItem(int position) {
    switch (position)
    {
        case 0: return new MondayFragment();
        case 1: return new TuesdayFragment();
        default: return null;
    }
}

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

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    switch (position)
    {
        case 0: return "Lunedì";
        case 1: return "Martedì";
        default: return "DEF_PAGE_TITLE";
    }
}

}

The layout of the main Activity is:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_below="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add_task_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:src="@drawable/round_add_black_48dp" />


</RelativeLayout>

Solution

  • To refresh adapter inside you fragment write this in you activity on Event

    MondayFragment myfragment =  (MondayFragment)  adapter.getItem(0);
    myfragment.refresh();
    

    And on MondayFragment Write Function

    public void  refresh(){
    
    if(taskAdapter != null){
    taskAdapter.notifydatasetchanged()
    }
    
    }