Android Question weird issue with File.Delete

RichyK68

Active Member
Licensed User
Longtime User
Hi,

I've got an activity with a listview that shows a list of files. On the _longclick event I pop up a messagebox to confirm deletion of the file. If I respond yes, it executes the File.Delete command on the appropriate file and removes the item from the listview. If I wait a few seconds then repeat the process again, it works.

However, if I longclick on a file, release, select yes, then immediately longclick, release, yes, longclick, release, yes etc then back out of the activity, I would expect the files to have been deleted. They haven't though (sometimes one or two of the files might be gone, sometimes none at all). The code is going through the motions but because I am deleting one file after another in quick succession, it really doesn't like it.

Can you shed some light on this?

Richard
 

LucaMs

Expert
Licensed User
Longtime User
Not all files are deleted or the listview item? Or both?

For first, I would try to use DoEvents (after File Delete).

For the listview, Invalidate.

If, instead, the problem is the slowness of the device in the delete files ... I do not think that there is an event "File Deleted" or similar
 
Upvote 0

RichyK68

Active Member
Licensed User
Longtime User
It's deleting fine from the listview without an invalidate, leading one to believe the files are also be being deleted, but they are still there when finishing the activity. Do you know what happens behind the scenes when File.Delete is called? It's almost like it can only deal with one file at a time, and if it's called multiple times, only one of them will get deleted, if you are lucky. I've tried doevents, first thing I thought of sadly, but that didn't work.

Cheers,

Richard
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I tried to run this:

B4X:
Sub DoIt
    Dim dir As String = File.DirDefaultExternal
    Dim FileNames As List
    FileNames.Initialize
  
    ' Creation
    For i = 0 To 9
        FileNames.Add("FileTest" & i)
        File.WriteString(dir, FileNames.Get(i), "Ciao")
    Next

    ' Deleting
    For i = 0 To 9
        File.Delete(dir, FileNames.Get(i))
    Next
End Sub

It works as it should (although, as usual, through the windows explorer would seem that things are not working, but with Eclipse it is all ok).

I guess you tried to execute your project step by step (F8) and checked the effect and the log (?)
 
Upvote 0

RichyK68

Active Member
Licensed User
Longtime User
It's all good, thanks. I couldn't see the forest for the trees. I had a one to one relationship between the listview and an internal list, but when deleting the files and the list item, I wasn't deleting from the internal list.
 
Upvote 0
Top