0

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?

Jan Horčička
  • 671
  • 1
  • 11
  • 26
  • You appear to be passing the wrong position to `notifyItemInserted()` (and I am nervous about your approach for `notifyItemRemoved()`). Beyond that, if I had to guess, your problems lie in the adapter code that you elected not to show. You might be happier [using `ListAdapter`](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/ListAdapter), which handles computing the differences between the old and new lists for you. – CommonsWare Oct 22 '22 at 22:23
  • I tried different positions for `notifyItemInserted()` including `size - 1` or not using it at all. What's wrong with `notifyItemRemoved()`? Isn't `RecyclerView` always a preferable option to `ListView`? – Jan Horčička Oct 22 '22 at 22:47
  • "What's wrong with notifyItemRemoved()?" -- there is no connection between `position` and your new data. You are assuming that `databaseHelper.getMedicationsList()` will return a new list with the element formerly in `position` removed, and that is not necessarily going to be the case. "Isn't RecyclerView always a preferable option to ListView?" -- yes, it is, which is why I linked to [the `ListAdapter` that works with `RecyclerView`](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/ListAdapter). – CommonsWare Oct 22 '22 at 22:50
  • I have overwritten the whole code to use ListAdapter and ViewModel that adds or removes items. But I am running into a similar issue. How do I retrieve the ViewModel from different activities or fragments so I can call its methods when I need to? – Jan Horčička Oct 23 '22 at 20:28
  • "How do I retrieve the ViewModel from different activities or fragments so I can call its methods when I need to?" -- for an activity and its fragments, you can use [a shared viewmodel](https://developer.android.com/guide/fragments/communicate#viewmodel). – CommonsWare Oct 23 '22 at 20:40

0 Answers0