0

I have two activities that each contain fragments. Activity A has a fragment with a ListView. Rows of the ListView are obtained from a SQLiteDatabase.

Activity B contains a fragment that enables entries of the database to be edited. When a user edits a database entry, saves the data, then returns to Activity A via the backbutton or:

actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);

How do I notify the ListView in Activity A that there has been a change to the database? I understand how to communicate between an Activity and it's fragments, but how do I implement a notifydatasetchanged when the fragments are in two different activities?

I suspect that going back to Activity A via the back button is simply recalling the view from the stack, but that view has changed when there is a database change.

user2254532
  • 1,851
  • 2
  • 14
  • 14
  • 2
    requery the dataset in onResume in the activyty A. – X3Btel Jul 06 '16 at 12:48
  • Possible duplicate of [Android ListView not refreshing after notifyDataSetChanged](http://stackoverflow.com/questions/14503006/android-listview-not-refreshing-after-notifydatasetchanged) – gevorg Jul 06 '16 at 12:53
  • Please consider accepting an answer as it may be helpful for other having the same question. Thanks. – kodartcha Aug 17 '17 at 09:14

3 Answers3

0

You could simply call the notifyDataSetChanged in onResume in your Activity A. So when you close or go back from Activity B to A, it will update the data. Don't forget to check if the adapter is different than null, otherwise it could crash the first time you open the App.

You could also make a public function in your Activity A which updates the list and call it from your Activity B.

class ActivityA

public static void updateList() {
    if(adapter != null){
      adapter.notifyDataSetChanged;
    }
}


class ActivityB

ActivityA.updateList();

Hope it helps :)

kodartcha
  • 1,063
  • 12
  • 23
0

You will need to read the updated dataset from your SQLite database and pass it adapter again. After you need to notify adapter. For example, like in the code below:

In your Activity A

@Override
protected void onResume() {
    super.onResume();

    // method to read new dataset
    Database ourDB = new Database(this);
    ourDB.openDB();
    data = ourDB.getData();
    ourDB.closeDB();

    if(adapter == null) {
        // initializes adapter when first launched
        adapter = new RecyclerViewAdapter(data, this);
        yourListView.setAdapter(adapter);
    }
    else {
        // in subsequent returns to activity we are replacing 
        // old data with new and reusing existing adapter
        adapter.updateData(data);
    }

}

In your adapter class include similar method:

public void updateData(ArrayList<String> newData) {
    data = newData;
    this.notifyDataSetChanged();
}
Marat
  • 6,142
  • 6
  • 39
  • 67
0

you could start activity with startActivityForResult(start activityB) and override method onActivityResult in activityA and notifyDataSetChanged when needed.