2

I have an SQLite database which I update from MainActivity and SecondActivity using DatabaseHelper.

In MainActivity I have a RecyclerView and an ArrayList with items from the database. In SecondActivity I have EditText to update/create data.

For example, in the DatabaseHelper.java class I have a method which deletes a note from the database:

public void deleteNote(String table, Note note) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(table, Note.COLUMN_ID + " = ?", new String[]{String.valueOf(note.getId())});
    db.close();
}

I can use it just fine in MainActivity where the RecyclerView is, by:

db.deleteNote("notes", notesList.get(position));
notesList.remove(position);
adapter.notifyItemRemoved(position);

Now, when I delete a note from SecondActivity:

private DatabaseHelper db = new DatabaseHelper(this);
List<Note> notesList = new ArrayList<>();
notesList.addAll(db.getAllNotes("notes")); //add all from "notes" table
//...
db.deleteNote("notes", notesList.get(intentExtra));

...and go back to MainActivity, the RecyclerView does not update. I have tried:

  • calling .notifyDataSetChanged() on the adapter;
  • calling .clear() on the arrayList and then adding items again, and calling .notifyDataSetChanged();
  • Using runOnUIThread in onResume to do the above;
  • Making the adapter in MainActivity static (memory leak and bugs) and updating from SecondActivity, which works most of the time...

...So, how do I properly update the RecyclerView in MainActivity when I change data in SecondActivity? Please let me know if I missed any details! Thank you!

EDIT:

After adding new data in SecondActivity and going back to MainActivity where the above methods are run in onResume() when I print the ArrayList, I don't see the newest item. However, if I open SecondActivity (not adding data) and go straight back - the new data is in the ArrayList and is shown in the RecyclerView. For some reason (probably caused by me) I have to open the activity twice before new data is shown.

Here's the screen recording in Youtube

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Akres
  • 126
  • 2
  • 16
  • Please post a piece of the code where you are trying to update data in list – Alexei Artsimovich Jun 02 '18 at 18:24
  • @AlexeiArtsimovich done. Please let me know if there's anything else! – Akres Jun 02 '18 at 18:33
  • No, I mean the method where you update the list like a onStart(), onPause() or something else. It would be even better if you post the whole code of MainActivity – Alexei Artsimovich Jun 02 '18 at 18:35
  • @AlexeiArtsimovich Oh, ok, understood. I have added the code. I don't update the list onStart(), nor onPause(). I create it once in OnCreate() in MainActivity, but in SecondActivity, I create a new list from db again. – Akres Jun 02 '18 at 18:42
  • Are you using `LoaderCallbacks` for loading the data into your `ArrayList` from your database? – Reaz Murshed Jun 02 '18 at 19:16
  • @ReazMurshed I only use "notesList.addAll(db.getAllNotes("notes"));" – Akres Jun 02 '18 at 19:19

2 Answers2

1

OK, As I understood you are not updating the list nor in onStart() neither in onPause(). OnCreate() gets called just once when the activity is creating and when you are back to this Acitivity only onStart() and then onPause() are called so you need to retrieve data from DB in onStart() method and populate your list again. Hope it will work!

Alexei Artsimovich
  • 1,074
  • 7
  • 15
  • I wish it did. The best thing about this fiasco is that I can add the data, go back, see that the recyclerView has not updated... then open the SecondActivity again (but don't add data) and go back. Data is shown. Same thing happened with onResume() - it only showed data after I reopened MainActivity. – Akres Jun 02 '18 at 19:04
  • https://stackoverflow.com/questions/3053761/reload-activity-in-android Maybe it's just Android "Gotcha" again. There seems to be no way to refresh the data without reloading the activity :/ – Akres Jun 02 '18 at 20:40
1

I have created a Github Project here to explain the details of updating a data in a table inside your sqlite database and then get the reflection in the list where you are showing the data. I have added separate activity to update the data of an item in the list and then when returned to the main activity, the list is updated with the latest updates. Hope you might take a look.

The whole purpose is to show how the database operations can be done using SQLite database along with an implementation of the content observer so that the update in the user table can be seen immediately in the list while you are registering for the content observer to the user table using the following command from your activity which contains the list.

this.registerContentObserver(cursor, DBConstants.DB_TABLE_USER_URI);

Once you are registering the observer, the list will be updated automatically on any change that you are making in your database table.

Please let me know if you have any further questions. Hope that helps.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98