I have apps where the requirement is update & delete the item of RecyclerView using dialog. The dialog will open after click the popup menu
I create the dialog function on Adapter class in onBindViewHolder. The function successfully update and delete the data on server. How do I refresh the RecyclerView after it?
Adapter.java
holder.cBtnMore.setOnClickListener(v -> {
PopupMenu popupMenu = new PopupMenu(v.getContext(), holder.cBtnMore);
popupMenu.getMenuInflater().inflate(R.menu.more_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(menuItem -> {
if (menuItem.getItemId() == R.id.menu_update) {
final Dialog dialog = new Dialog(v.getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_update);
dialog.getWindow().setLayout(Cons.widthScreen, Cons.heightScreen);
// Declaration & Set Text
...
btnUpdate.setOnClickListener(unused -> {
updateBarang(
v.getContext(),
id,
etNama.getText().toString(),
etAlamat.getText().toString(),
etNoPenjual.getText().toString(),
etKodeBarang.getText().toString(),
etJumlahPenjualan.getText().toString(),
etHargaSatuan.getText().toString(),
etDiskon.getText().toString(),
etTotalHarga.getText().toString()
);
});
btnCancel.setOnClickListener(unused -> {
dialog.dismiss();
});
dialog.show();
} else if (menuItem.getItemId() == R.id.menu_delete) {
Toast.makeText(v.getContext(), "Delete", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "Error", Toast.LENGTH_SHORT).show();
}
return true;
});
popupMenu.show();
});
MainActivity.java
public class MainActivity extends AppCompatActivity {
FloatingActionButton fabAdd;
RecyclerView rvBarang;
ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apiInterface = ApiConnection.Connection().create(ApiInterface.class);
...
rvBarang.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onResume() {
Call<List<Barang>> tampilBarang = apiInterface.listBarang();
tampilBarang.enqueue(new Callback<List<Barang>>() {
@Override
public void onResponse(Call<List<Barang>> call, Response<List<Barang>> response) {
ArrayList<Barang> barangArrayList = (ArrayList<Barang>) response.body();
BarangAdapter barangAdapter = new BarangAdapter(barangArrayList);
rvBarang.setAdapter(barangAdapter);
}
@Override
public void onFailure(Call<List<Barang>> call, Throwable t) {
// TODO
}
});
super.onResume();
}
}

If you want to update your recyclerview then you have to update your data source of adapter means list data which you are using in adapter to show and after that you need to notify adapter to refresh
To update recyclerview after delete , add below method in your adapter class
To refresh recyclerview after update , you must have position along with updated object so add below method also in adapter class
after adding above methods , you can just call it to refresh