I've read the following:
ArrayList not updating after using .clear()
notifydataSetChanged working but only showing 1 result in listview Android
notifyDataSetChanged() does not affect my adapterView
Android: notifyDataSetChanged(); not working
notifydatasetchanged() not working after onbackpressed()
Android notifyDataSetChanged not working
Android notifyDataSetChanged() not working
And still can't get my adapter to update from a new data set.
My code in debug mode is as follows:
public class PetInformationActivity extends AppCompatActivity
implements ConfirmDialogFragment.ConfirmDialogListener, MedicalInformationFragment.OnFragmentInteractionListener{
private static List<Assignment> listAssignments = new ArrayList<>();
private static AssignmentsAdapter mAdapter;
private static PetInformationViewModel sPetInformationViewModel;
.
.
.
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
.
.
.
/**
* get the providers' IDs assigned to the pet: fillAssignmentsArray()
*/
listAssignments = sPetInformationViewModel.getAssignedProviders(petId);
In debug at this point:
mAdapter = null
listAssignments = (ArrayList@5411) size = 0
Log.d(TAG, "listAssignments has " + listAssignments.size() + " Assignment objects in it");
Log.d(TAG, "listAssignments is: " + listAssignments);
In debug at this point:
mAdapter = null
listAssignments = (ArrayList@5461) size = 1
0 = (Assignment@5496)"Assignment{mType='Veterinarian', mProviderName='red vet'}"
mAdapter = new AssignmentsAdapter(this, listAssignments);
In debug at this point:
mAdapter = (AssignmentsAdapter@5499)
listAssignments = (ArrayList@5461) size = 1
0 = (Assignment@5496)"Assignment{mType='Veterinarian', mProviderName='red vet'}"
mRecyclerView.setAdapter(mAdapter);
.
.
.
}
public void onResume(){
super.onResume();
Log.d(TAG, "Entered: onResume");
listAssignments.clear();
Log.d(TAG, "listAssignments is: " + listAssignments);
In debug at this point:
mAdapter = (AssignmentsAdapter@5499)
listAssignments = (ArrayList@5461) size = 0**
listAssignments = sPetInformationViewModel.getAssignedProviders(petId);
Log.d(TAG, "listAssignments is: " + listAssignments);
In debug at this point:
mAdapter = (AssignmentsAdapter@5499)
listAssignments = (ArrayList@5594) size = 1
0 = (Assignment@5607)"Assignment{mType='Veterinarian', mProviderName='red vet'}"
mAdapter.notifyDataSetChanged();
}
At this point the screen is blank when it should show the text shown in Assignment@5607. It appears that the notififyDataSetChanged() notified mAdapter to the change from the text shown in onCreate (ArrayList@5461) to clear in onResume (ArrayList@5461) but not for the new call for data in onResume (ArrayList@5594).
This seems to indicate that when mAdapter was initialized to listAssignments it was set to use the ArrayList at address @5461. When a call for new data was made in onResume, at a) return from use of the Back button and (b) when this Activity first starts, a new ArrayList was created at a different address.
Therefore, it seems that I have not incorporated my ArrayList properly as all updates to listAssignments will result in a brand new ArrayList object that is never used to update mAdapter, which will always be updated to the initial ArrayList at address @5461.
I have searched for information as to how to properly bind an Adapter to an ArrayList but I haven't found anything that alludes to more than I have done. I assume that the intention for notifyDataSetChanged() is to update using an existing Adapter so that the existing Adapter doesn't have to be destroyed and a new one created at each data set change as inferred here: