Search code examples
javaandroidandroid-fragmentsandroid-activityandroid-sqlite

How do I update my Fragment after data modification in my activity (SQLite)?


I have a ListView in a Fragment that is populated when the app starts. I put a ParcelableArrayList in a Bundle in my newInstance method, and I get it back in my OnCreateView after passing the ArrayList in the newInstance method in my Activity (which is the data read from the SQLite database).

This part works, as I display my data in my Fragment correctly.

I implemented a button that removes all data from the table, and I would now like to update my view after I cleaned the table. The remove all button is handled in my main activity where I call my database handler to empty the table.

What is the best way to do that ? Here are the parts of the code that seem relevant to me :

My Fragment class :

public class MainFragment extends Fragment {

    public static final String RECETTES_KEY = "recettes_key";
    private List<Recette> mRecettes;
    private ListView mListView;

    public MainFragment() {
        // Required empty public constructor
    }

    public static MainFragment newInstance(List<Recette> r) {
        MainFragment fragment = new MainFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(RECETTES_KEY, (ArrayList<? extends Parcelable>) r);
        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRecettes = getArguments().getParcelableArrayList(RECETTES_KEY);
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        configureListView();
    }

    // Configure ListView
    private void configureListView(){
        this.mListView = getView().findViewById(R.id.activity_main_list_view);
        RecetteAdapter adapter = new RecetteAdapter(getContext(), mRecettes);
        mListView.setAdapter(adapter);
    }
}

Relevant parts from my Main acivity : This is in my OnCreate method :

mDatabaseHandler = new DatabaseHandler(this);
mRecettes = mDatabaseHandler.readRecettes();
mDatabaseHandler.close();

This is in the method I use to show a fragment :

if (this.mMainFragment == null) this.mMainFragment = MainFragment.newInstance(mRecettes);
this.startTransactionFragment(this.mMainFragment);

Let me know if I should add more of my code, this is my first time posting :) Lucile


Solution

  • In your R.layout.fragment_main, you can add an id to the root view, say with android:id@+id/fragment_root

    And whenever you want to change the fragment view:

    In activity:

    MainFragment fragment = (MainFragment) getSupportFragmentManager().getFragmentById(R.id.fragment_root);
    fragment.updateList(mRecettes);
    

    And then create the new method updateList() in your MainFragment

    public void updateList(List<Recette> recettes) {
        mRecettes.clear();
        mRecettes.addAll(recettes);
        configureListView();
    }
    

    Also you can tag your fragment when you add it to your transaction instead of using its id, and then use getSupportFragmentManager().getFragmentByTag()