I have created an android application which is able to update my firebase database. I have managed to connect the database to my application, fetch the data, edit it and save it. However, in some cases the application crashes because of the following error:
Process: com.example.codelibadministrator, PID: 1883
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131230830, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
I have done some research and most of the solutions I have come across state that I need to call the notifyDataSetChanged() method, which I am doing, yet the error still occurs.
I display the information from the database on a listview which is in a fragment. Clicking on one of the items of the list view will start a new activity from where the information about that certain item can be edited or deleted. After changes are made (or are not made) or the certain item is deleted the application returns to the fragment with the list view calls the notifyDataSetChanged() method and updates the listview. What is the issue with my code?
The following is a method responsible for fetching the data from the database and updating the listview with the fetched data. It is called when the fragment is created and when the application returns from the activiry responsible for editing data.
private void fetchAndDisplayData() {
list.clear();
idList.clear();
ref.addValueEventListener(new ValueEventListener() {
// fetch data from database
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
books = ds.getValue(Book.class);
String book = books.getTitle();
String bookId = books.getPostId();
boolean isDuplicate = false;
for (int i = 0 ; i < list.size(); i++) {
if (list.get(i) == book) {
isDuplicate = true;
}
}
if (!isDuplicate) {
list.add(book);
idList.add(bookId);
}
/*idList.add(books.getPostId());*/
}
listView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
My on activity result method which is called when the activity responsible for editing the information for a single item finishes. My onDataSetChanged() method is called here before refilling the listview with the updated data.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == DATA_CHANGED && resultCode == Activity.RESULT_OK) {
listView.setAdapter(null);
adapter.notifyDataSetChanged();
fetchAndDisplayData();
}
}
EDIT: Error seems to be occurring when an item is accessed and no changes are made to the item's infromation. The activity will finish and return to the list view, when I attempt to access the same item again, or another item I receive the error.
EDIT2: After some debugging I have noticed that the onActivityResult part of my code is never executed. This may be why my problem occurs. Is it possible that this code is never executed because I am starting a new activity from a fragment?
EDIT3: So I have found the problem of my question. The onActivityResult method in the fragment is never accessed because when the second activity returns its result it returns it to the onActivityResult method of the MainActivity where the fragment which calls the second activity is located. The answer to my problem now shifts to "How can I send information from the onActivityResult() method of the main activity to the onActivityResult() of a fragment in the same activity?"