5

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...

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Farhan
  • 3,144
  • 13
  • 44
  • 60

3 Answers3

1

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

Community
  • 1
  • 1
Justin Shield
  • 2,360
  • 14
  • 12
  • @Justin.. thankyou so much for your help.. Got it fixed.. One more problem sir. The files from the gallery are now removed and media scanner is also working fine. But I was unable to refresh my list using notifydatasetChanged().. I need to refresh the activity.. Is there any way to restart the activity again??? If you can help me here, almost all issues of my application will be resolved.. Hope you will have some clue about it as well... – Farhan Jul 08 '11 at 10:38
0

You are adding the file names from the specific folder into the ArrayList Or some kind of list to let it show onto the ListView. So if it is an ArrayList then upon deletion of that File i.e. file.delete() there are two cases:

  1. You should also Remove the Specific name from the ArrayList and then call the notifyDataSetChanged() method. using arraylistname.remove(int index);

  2. In 2nd case if you delete your file and File has been deleted successfully from the Specific Folder then you have to initialize the ArrayList with Zero Data and again call the method that will regenerate all your File Names into the ArrayList and then call notifydatasetChanged method. by using this if the file has been deleted and you call the method to read the file names from the specific folder, the deleted file will not be inyour list and when you add all the file names into the list and call notifydatasetchanged it will Update your List.

Hope this Helped.

Just comment down if you did not get my point. or need any more help.

Shah
  • 4,832
  • 9
  • 44
  • 70
  • Even if we forget about listview at the moment. The problem is that the video files that I deleted are deleted from the DCIM folder but they are still shown in the Gallery.. I think there is some problem with updating the MediaStore because when I delete any file from my system; the mediascanning is done and after that when I run my application, all deleted items are automatically removed.. – Farhan Jul 08 '11 at 09:27
0

I suppose you'd need to force rescanning of image/video mediastore, since when you delete file gallery doesn't show'em as deleted. Am I right?

Please read carefully this discussion

Community
  • 1
  • 1
Barmaley
  • 16,009
  • 17
  • 68
  • 139
  • thanks sir for your answer. Can you tell me how to update or refresh the listview. I have used notifyDataSetChange() but I need to refresh the Activity so that my list is up-to-date. Can you help me?? – Farhan Jul 08 '11 at 10:39