开发者

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


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜