Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Files not removed upon delete and still shown in Listview

I have made a program which populates a listview with videos stored on the device and plays them. It have also tried to implement the functionality of deleting the file using file.delete function. However, after using notifydatasetchange() function, the file is still shown in the listview. Moreover, I just noticed that the video file was deleted from the DCIM folder; however, it is showing up in the Gallery of the device but when you click it, it can't be played.. Here is the part of code where I am showing a dialog box to the user, when the user clicks Yes , then the delete function is performed.

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                    case DialogInterface.BUTTON_POSITIVE:

                         file.delete();
                         adapter.notifyDataSetChanged();
                         //list.setAdapter(adapter);
                         break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        image2.setImageResource(R.drawable.play);
                        flag.setText("play");
                        //No button clicked
                        break;
                    }
                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("This Item will be Deleted\nAre you sure?").setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener).show();


        }// else closes 

I don't understand why it is happening. Any useful suggestions please...

like image 795
Farhan Avatar asked Nov 13 '22 20:11

Farhan


1 Answers

I think I see the problem.

You're deleting the file, however you actually need to remove the corresponding Array element from the ArrayListAdapter and then call notifyDataSetChanged on your adapter.

See:

update listview dynamically with adapter

To force the media scanner to run again see:

Android file delete leaves empty placeholder in Gallery

You'll also need to take into account the following:

notifyDataSetChanged example

like image 142
Justin Shield Avatar answered Feb 16 '23 00:02

Justin Shield