I'm using recyclerview to display local images and videos from sd card when I delete any video or image from my fragment it is deleted but it stays there. I have to go back to show the change in data. My recycler view doesn't refresh automatically doesn't refresh automatically. Can anyone tell me how to fix it Here is my code.
RecyleAdapter.java
public class SavedRecycleAdapter extends RecyclerView.Adapter<SavedRecycleAdapter.MyViewHolder> {
private static String DIRECTORY_SAVE_MEDIA = "/storage/emulated/0/Folder/";
private ArrayList <File> filesList;
private Activity activity;
Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView videosThumbnail, picturesThumbnail;
ImageButton imageDelete, imageShare, videoShare, videoDelete;
CardView videoCardView, imageCardView;
public MyViewHolder(View itemView) {
super(itemView);
picturesThumbnail = itemView.findViewById(R.id.imagesThumbnailSaved);
videosThumbnail = itemView.findViewById(R.id.videosThumbnailSaved);
imageDelete = itemView.findViewById(R.id.imageDeleteSaved);
imageShare = itemView.findViewById(R.id.imageShareSaved);
videoDelete = itemView.findViewById(R.id.videoDeleteSaved);
videoShare = itemView.findViewById(R.id.videoShareSaved);
videoCardView = itemView.findViewById(R.id.videoCardViewSaved);
imageCardView = itemView.findViewById(R.id.imageCardViewSaved);
}
}
public SavedRecycleAdapter(ArrayList <File> filesList, Activity activity) {
this.filesList = filesList;
this.activity = activity;
}
@Override
public SavedRecycleAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.saved_item_view, parent, false);
return new SavedRecycleAdapter.MyViewHolder(inflatedView);
}
@Override
public void onBindViewHolder(final SavedRecycleAdapter.MyViewHolder holder, int position) {
final File currentFile = filesList.get(position);
if (currentFile.toString().endsWith(".mp4")) {
holder.videoCardView.setVisibility(View.VISIBLE);
holder.imageCardView.setVisibility(View.GONE);
Glide.with(activity)
.load(Uri.fromFile(new File(currentFile.getAbsolutePath())))
.into(holder.videosThumbnail);
holder.videoDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(currentFile.getAbsolutePath());
boolean deleted = file.delete();
//
}
});
holder.videosThumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(activity, VideoViewerActivity.class);
intent.putExtra("video", currentFile.getAbsolutePath());
activity.startActivity(intent);
}
});
holder.videoShare.setOnClickListener(new View.OnClickListener() {
//Do stuff
} else {
holder.videoCardView.setVisibility(View.GONE);
holder.imageCardView.setVisibility(View.VISIBLE);
Glide.with(activity).load(currentFile.getAbsolutePath())
.skipMemoryCache(false)
.crossFade().into(holder.picturesThumbnail);
holder.imageDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(currentFile.getAbsolutePath());
boolean deleted = file.delete();
holder.picturesThumbnail.setOnClickListener(new View.OnClickListener() {
// Do stuff
holder.imageShare.setOnClickListener(new View.OnClickListener() {
}
}
@Override
public int getItemCount() {
return filesList.size();
}
}
And here is my code for fragment
public class SavedStoriesFragment extends Fragment {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
private static final String FILE_PATH = "/storage/emulated/0/Folder/";
public SavedStoriesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_saved_stories, container, false);
recyclerView=(RecyclerView)v.findViewById(R.id.savedRecyclerView);
layoutManager = new GridLayoutManager(getContext(),2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new ItemDecorationAlbumColumns(
getResources().getDimensionPixelSize(R.dimen.image_list_space),2));
getListFiles(new File(FILE_PATH));
SavedRecycleAdapter ra = new SavedRecycleAdapter(this.getListFiles(new File(FILE_PATH)), getActivity());
recyclerView.setAdapter(ra);
return v;
}
private ArrayList<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files;
files = parentDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().endsWith(".jpg") ||
file.getName().endsWith(".png") ||
file.getName().endsWith(".jpeg")||
(file.getName().endsWith(".mp4") ||
file.getName().endsWith(".gif"))) {
if (!inFiles.contains(file))
inFiles.add(file);
}
}
}
return inFiles;
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> mFragment = new ArrayList<>();
ArrayList<String> mTitles = new ArrayList<>();
public void addFragments (Fragment fragment,String tabTitles)
{
this.mFragment.add(fragment);
this.mTitles.add(tabTitles);
}
public ViewPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragment.get(position);
}
@Override
public int getCount() {
return mFragment.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
}
}
When you are deleting a file, make sure that you update the list which holdsnits reference. In your case, most probably after deleting the file from storage, its reference still exists in the list and your RecyclerView still displays the list item view for it.
Eg: If you are saving all the references of files in the list named mFileList, then you need to delete the reference of deleted file using mFileList.remove() and then use notifyDataSetChanged() to update the recycler view.