I have an AddMedication
activity which adds a new item to a database and returns to a main screen fragment which contains a RecyclerView list of items. The problem is that the list doesn't get updated with the newly added item.
I have tried notifyDataSetChanged()
and notifyItemInserted(medicationList.size())
but without luck. I have somehow managed to make item removal work with notifyItemRemoved(position)
.
This is a part of my MedicationAdapter
:
public class MedicationAdapter extends RecyclerView.Adapter<MedicationAdapter.ViewHolder> {
....
....
@Override
public int getItemCount() {
return medicationList.size();
}
public void removedItem(int position)
{
List<Medication> newList = databaseHelper.getMedicationsList();
medicationList.clear();
medicationList.addAll(newList);
this.notifyItemRemoved(position);
}
public void addedItem(Medication medication) {
medicationList.add(medication);
this.notifyDataSetChanged();
this.notifyItemInserted(medicationList.size());
}
The addedItem
gets called in the AddMedication
activity.
private void saveMedication() throws Exception {
...
database data preparation
...
Medication medication = new Medication(name, diseases, expirationDate, tabletsAmount);
databaseHelper.addMedication(medication);
MedicationAdapter medicationAdapter = new MedicationAdapter(getApplicationContext());
medicationAdapter.addedItem(medication);
}
Any idea why the list is not updated?