I have a Recyclerview in a fragment and i would like to add the option to delete a playlist that has been added to MediaStore.Audio.Playlists.
Method that creates the Recyclerview and gets called in onCreateView()
private void initRecyclerView(){
items = Main.songs.getPlaylistNames();
if ((items != null) && (! items.isEmpty())) {
adapter = new Adapter(getActivity(), items);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerViewPlaylist.setLayoutManager(linearLayoutManager);
recyclerViewPlaylist.setHasFixedSize(true);
recyclerViewPlaylist.setAdapter(adapter);
Log.i(TAG, "Playlist found in list!" + items.size());
recyclerViewPlaylist.addOnItemTouchListener(new OnItemClickListeners(getActivity(), recyclerViewPlaylist, new OnItemClickListeners.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getActivity(), "You Clicked position: " + position + " " + items.get(position) , Toast.LENGTH_SHORT).show();
if (! Main.songs.isInitialized()){
return;
}
String selectedPlaylist = items.get(position);
Main.musicList = Main.songs.getSongsByPlaylist(selectedPlaylist);
Intent intent = new Intent(getActivity(), ListSongsActivity.class);
intent.putExtra("title", selectedPlaylist);
startActivity(intent);
}
@Override
public void onLongClick(View view, int position) {
currentIndex = position;
drawerLayout5.openDrawer(Gravity.END);
}
}));
}else{
Log.i(TAG, "No playlist found in list!");
}
NavigationView with an option menu, for now i only implemented a remove option.
mNavigationView5.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
switch (item.getItemId()){
case R.id.ic_edit:
break;
case R.id.ic_info:
break;
case R.id.ic_remove:
final Dialog dialog;
dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.custom_dialog);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
ImageButton imageButtonCheck = dialog.findViewById(R.id.checkBtn);
ImageButton imageButtonClose = dialog.findViewById(R.id.closeBtn);
TextView textView = dialog.findViewById(R.id.dialogTitle);
String name = items.get(currentIndex);
String text = getString(R.string.delete_warning, name);
textView.setText(text);
imageButtonCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Main.songs.removePlaylist(getActivity(), items.get(currentIndex));
items = Main.songs.getPlaylistNames();
adapter.removeAt(currentIndex);
}catch (Exception e){
Log.e(TAG, "Exception caught when removing selected playlist!" + e.toString());
}
}
});
imageButtonClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
dialog.show();
break;
}
return false;
}
});
Remove method in my Adapter class
public void removeAt(int position) {
list.remove(position);
notifyItemRemoved(position);
}
Now i for sure now that Main.songs.removePlaylist(getActivity(),items.get(currentIndex));
is not the problem because when i remove a playlist, close the app and then restart it, it is removed.
But my problem is that when my app is running and i switch tabs in my tabbed activity with ViewPager the removed Playlist reappears.
I think my problem has something to do with preloaded tabs, i noticed that in a ViewPager all tabs get preloaded.
ps. items is a private ArrayList items;