My Recycler view does not update when I add or remove a new item to it. How do I fix that?
I looked at this question here, which seems to have all the answers but they are not working for me --> link
code:
public void createPlaylist() {
dialog.setContentView(R.layout.add_playlist);
final TextView Playlist_name = dialog.findViewById(R.id.Playlist_name);
Button CreateButton = dialog.findViewById(R.id.CreateButton);
CreateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = Playlist_name.getText().toString().trim();
if (TextUtils.isEmpty(text)) {
Playlist_name.requestFocus();
Playlist_name.setError("Please fill in this field");
} else {
boolean inserted = db.addPlaylist(text); // **How do I update the playlist after its added to the db??**
if (inserted) {
Toast.makeText(getContext(), "Playlist created!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "Playlist isn't created :(", Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
CODE: This is within my viewholder insinde my Custom Adapter class
holder.popupMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(context, v);
popupMenu.inflate(R.menu.popup_menu);
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.DialogStyle);
dialog.setTitle("Delete Playlist")
.setMessage("Are you sure you want to delete this playlist?")
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
db.deletePlaylist(array_list.get(position).getPlaylistName()); **//How do I update the recycler after deleting ???**
Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", null);
dialog.create().show();
return true;
default:
return false;
}
}
});
}
});