Search code examples
javaandroidfileandroid-recyclerviewandroid-file

Why is only one file getting deleted when I click to delete multiple?


I am trying to make a program where I list the file names in a recyclerview. Using a button inside the row, the application should delete the item via a path which is stored in an array that includes the file path. The application should also display the file path if the card itself is clicked in a toast message.

So far I am able to do this, however I can only delete one file at a time when the application is active. The problem arises when I try to delete multiple files. The items are deleted from the recyclerview and everything looks normal, but if the application is closed then the files that I attempted to delete after the first one show up again, indicating that they were not deleted.

My code:

JAVA CLASSES

FileView.java (this is the main activity)

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class FileView extends AppCompatActivity {

    private RecyclerView fileRecyclerView;
    private RowAdapter fileAdapter;
    private RecyclerView.LayoutManager fileLayoutManager;
    private ArrayList<RowItem> rowItem;
    File[] fileList;
    String filePath = "";
    String fileData = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_view);
        filePath = "PDF_files";
        File file = new File(getExternalFilesDir(filePath).toString());
        fileList = file.listFiles();
        createRows();
        buildRecyclerView();
    }


    public void createRows(){

        rowItem = new ArrayList<>();
        for (int i = 0; i < fileList.length; i++) {
            rowItem.add(new RowItem(R.drawable.ic_book,(fileList[i].getName().replace("__", " ").replace('_','\n').replace('-','/').replace(".pdf",""))));
        }
    }

    public void removeItem(int position) {
        rowItem.remove(position);
        fileAdapter.notifyItemRemoved(position);
    }

    public void buildRecyclerView() {

        fileRecyclerView = findViewById(R.id.recyclerView);
        fileRecyclerView.setHasFixedSize(true);
        fileLayoutManager = new LinearLayoutManager(this);
        fileAdapter = new RowAdapter(rowItem);
        fileRecyclerView.setLayoutManager(fileLayoutManager);
        fileRecyclerView.setAdapter(fileAdapter);

        fileAdapter.setOnItemClickListener(new RowAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                fileData = fileList[position].toString();
                Toast.makeText(FileView.this,"Clicked: " + fileData , Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDeleteClick(int position) {
                removeItem(position);

                File deletePath = fileList[position];
                deletePath.delete();

                if(deletePath.exists()){
                    getApplicationContext().deleteFile(deletePath.getName());
                }

            }
        });
    }
}

RowAdapter.java (this is the adapter class)

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class RowAdapter extends RecyclerView.Adapter<RowAdapter.RowViewHolder> {

    private ArrayList<RowItem> mRowList;
    private OnItemClickListener mListener;

    public interface OnItemClickListener{
        void onItemClick(int position);
        void onDeleteClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }

    public static class RowViewHolder extends RecyclerView.ViewHolder{
        public ImageView rowImageView;
        public TextView rowTextView;
        public ImageView rowDeleteImage;

        public RowViewHolder(@NonNull View itemView, OnItemClickListener listener) {
            super(itemView);
            rowImageView = itemView.findViewById(R.id.fileImage);
            rowTextView = itemView.findViewById(R.id.fileName);
            rowDeleteImage = itemView.findViewById(R.id.deleteFile);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null){
                        int position = getAdapterPosition();
                        if(position != RecyclerView.NO_POSITION){
                            listener.onItemClick(position);
                        }
                    }
                }
            });

            rowDeleteImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null){
                        int position = getAdapterPosition();
                        if(position != RecyclerView.NO_POSITION){
                            listener.onDeleteClick(position);
                        }
                    }

                }
            });

        }
    }

    public RowAdapter(ArrayList<RowItem> rowList){
        mRowList = rowList;
    }

    @NonNull
    @Override
    public RowViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
        RowViewHolder rvh = new RowViewHolder(v, mListener);
        return rvh;
    }

    @Override
    public void onBindViewHolder(@NonNull RowViewHolder holder, int position) {
        RowItem currentItem = mRowList.get(position);

        holder.rowImageView.setImageResource(currentItem.getImageResource());
        holder.rowTextView.setText(currentItem.getFileName());
    }

    @Override
    public int getItemCount() {
        return mRowList.size();
    }
}

RowItem.java (getters for image and filename)

public class RowItem {
    private int imageResource;
    private String fileName;

    public RowItem(int img, String stringInput) {
        this.imageResource = img;
        this.fileName = stringInput;
    }

    public int getImageResource() {
        return imageResource;
    }

    public String getFileName() {
        return fileName;
    }
}

XML FILES

activity_file_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.loopbreakr.firstpdf.FileView">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="4dp"
        android:scrollbars="vertical"
        android:background="@color/cardview_shadow_start_color"
        />
</RelativeLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_marginBottom="4dp"
    app:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp">
        <ImageView
            android:id="@+id/fileImage"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:padding="2dp"/>

        <TextView
            android:id="@+id/fileName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="55dp"
            android:layout_marginTop="5dp"
            android:text="File Name"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/deleteFile"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_margin="5dp"
            android:padding="2dp"
            android:src="@drawable/ic_delete" />
    </RelativeLayout>
</androidx.cardview.widget.CardView>

EDIT I tried to change the array to an arraylist as suggested, however I think I must have done so incorrectly as this results in a crash.

FileView.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileView extends AppCompatActivity {

    private RecyclerView fileRecyclerView;
    private RowAdapter fileAdapter;
    private RecyclerView.LayoutManager fileLayoutManager;
    private ArrayList<RowItem> rowItem;
    List<File> fileList;
    String filePath = "";
    String fileData = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_view);
        filePath = "PDF_files";
        File file = new File(getExternalFilesDir(filePath).toString());
        fileList = Arrays.asList(file.listFiles());
        createRows();
        buildRecyclerView();
    }


    public void createRows(){

        rowItem = new ArrayList<>();
        for (int i = 0; i < fileList.size(); i++) {
            rowItem.add(new RowItem(R.drawable.ic_book,(fileList.get(i).getName().replace("__", " ").replace('_','\n').replace('-','/').replace(".pdf",""))));
        }
    }

    public void removeItem(int position) {
        rowItem.remove(position);
        fileAdapter.notifyItemRemoved(position);
    }

    @Override
    public void recreate() {
        super.recreate();
    }

    public void buildRecyclerView() {

        fileRecyclerView = findViewById(R.id.recyclerView);
        fileRecyclerView.setHasFixedSize(true);
        fileLayoutManager = new LinearLayoutManager(this);
        fileAdapter = new RowAdapter(rowItem);
        fileRecyclerView.setLayoutManager(fileLayoutManager);
        fileRecyclerView.setAdapter(fileAdapter);

        fileAdapter.setOnItemClickListener(new RowAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                fileData = fileList.get(position).toString();
                Toast.makeText(FileView.this,"Clicked: " + fileData , Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDeleteClick(int position) {
                removeItem(position);

                File deletePath = fileList.get(position);
                deletePath.delete();
                if(deletePath.exists()){
                    getApplicationContext().deleteFile(deletePath.getName());
                }

                fileList.remove(position);
            }
        });
    }
}

Solution

  • You make it yourself difficult as you keep two lists.

    rowItems and fileList. 
    

    In RowItems you have only a String variable fileName.

    Add a variable String filePath and use it instead of the file list array.