0

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);
}

}

Faisal Khan
  • 353
  • 1
  • 16
  • As long as I've seen there are 2 possibilities that `notifyDataSetChanged()` is not working. First is, adapter loses its reference to the data so the actual list is not updated. Try creating a method inside the adapter to change the list and then call `notifyDataSetChanged()` to see if it works. If it doesn't work, views have a method called `post()` which are fired when they become available. Try to do your work there, it will be updated. – Furkan Yurdakul Jan 29 '19 at 14:58
  • The `post()` method I've mentioned, you can call it like `recyclerView.post(() -> adapter.notifyDataSetChanged());` on Java 1.8 – Furkan Yurdakul Jan 29 '19 at 15:00

4 Answers4

2

You need to use notifyDataSetChanged() or notifyItemRemoved(position) to notify your adapter that underlying data has been changed or particular item is removed. You can read more about the methods from here.

karan
  • 8,637
  • 3
  • 41
  • 78
  • Tried to do that but my app crashed . Can u please tell me where to add it – Faisal Khan Jan 29 '19 at 11:01
  • you can call it from your adapter once you have deleted your data – karan Jan 29 '19 at 11:04
  • if you have already tried it and there is a crash edit your question with error log it will be more helpful to answer. – karan Jan 29 '19 at 11:04
  • i have tried to notifyDataSetChanged() and notifyItemRemoved(position) everywhere but it doesnt refresh please tell me exactly where to add it. I have been searching for past 4 hours on google but nothing helps. Also i have added my code to viewpage Adapter – Faisal Khan Jan 29 '19 at 14:53
1

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.

Pankaj Sati
  • 2,441
  • 1
  • 14
  • 21
  • that works thanks, it only solved my one problem but when i download file from one fragment it doesn't show in the other fragment – Faisal Khan Jan 29 '19 at 16:08
  • If this answer solved the problem, please mark it as an answer so that others can also see it. Regarding your other problem of download, you can either use interfaces or fragment communication. This link will help you https://stackoverflow.com/a/13701071/10602679 – Pankaj Sati Jan 29 '19 at 18:22
1

I have same issue, but i have solution for that if you using ViewPager from androidx. You can change from :

public class ViewPagerAdapter extends FragmentPagerAdapter

to :

public class ViewPagerAdapter extends FragmentStatePagerAdapter
backids99
  • 11
  • 1
  • 2
0

after delete some thing call fragment method to get new ListFiles and notifyDataSetChanged()

as example

inside fragment

public void refreshRecyclerView() {
   ra.setFilesList(this.getListFiles(new File(FILE_PATH)))
}

inside adapter

public void setFilesList(ArrayList <File> filesList) {
        this.filesList = filesList;
        notifyDataSetChanged();
    }

or simply you can remove item from filesList and call notifyDataSetChanged() inside adapter

J.D
  • 58
  • 6
  • I have tried to notifyDataSetChanged() and notifyItemRemoved(position) everywhere but it doesnt refresh please tell me exactly where to add it. I have been searching for past 4 hours on google but nothing helps. Also i have added my code to viewpage Adapter – Faisal Khan Jan 29 '19 at 14:53