Search code examples
android-adapter

update data in main after changes made in adapter


I have the mainactivity and its adapter, inside the adapter I have a dialog in which the user edit holder's data, so once the save button is clicked the changes are saved on firebase and I call notifydatachanged, now the problem is that in mainactivity I retrieve data from firebase, but the function is not called once the dialog disappear, because the activity it's not changed so the oncreate it's the same. There is a way to alert the mainactivity from adapter that data changed?

A solution is to make the activity reload like this:

 ((AnimeActivity)context).recreate();

But the reload flash it's ugly to see, so I'd like to do in another way if is possible.


Solution

  • It's enough to call the function that retrieve data from firebase, from the adapter, but you need to reset the adapter's data list, example:

    Inside the Activity class:

    public void updateData() {
        data_set.clear();
        //code to retrieve data from firebase and then 
        adapter.setDataSet(data_set);
        recycler_view.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
    

    Inside the Adapter class:

    ((Activity)context).updateData();