Android Question Detect type of Object of CustomListView_ItemClick

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to Everybody here.
I would like to know whether it exists a way to understand the type of Object arriving from CustomListView_ItemClick(index,Object). This need arises from the following situation:
I have a CustomListView populated by file names, and file Objects. Some of the objects are obtained using File.ListFiles(dir) , while others are obtained reading a folder on an SD card, with ExternalStorage management. In other words, some are "normal" files, others are ExternalFile. When I select an item, the CustomListView_ItemClick sends an object as the value parameter.
If I want to read the file, I have to use (I suppose) different managements and therefore I need to know the "type" of the object. In the first case I will read files with usual procedure, while for the external file i need to use ExternalStorage etc. (all this, of course, if I don't miss something, as usual..)
BTW there may exist other solutons. Maybe I can keep two separate lists, containing only the first and only the second type of file, as a support of the CustomListView, and manage the problem. Or I can use Exception management for a "dirtier" solution, trying to cast the externalFile etc.. These are not my first choices, of course.
Thanks in advance
 
Solution
use Is to test the type.
B4A allows you to test the type of an object using the Is operator.
Example:
If Value Is ExternalFile Then
    ' This is a file from ExternalStorage
Else If Value Is String Then
    ' It's a normal file (path)
End If

But be careful: ExternalFile is not a native B4A type.
ExternalStorage files are generally JavaObjects.

So you can do:
Example:
If Value Is JavaObject Then
    ' External file
Else If Value Is String Then
    ' Normal file
End If
untested code

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Answering to my question with a simple trick. I use the following code, which, for my specific need is enough:
B4X:
Private Sub ListaFiles_ItemClick (Index As Int, Value As Object)
   dim NomeDelFile as string
   NomeDelFile=Value ' maybe simply a filename for first type of object, or an externalfile
    If NomeDelFile.Contains("Name=") Then '  is as externalFile, therefore contains other "particular" items, like "name=" ...
        Dim z() As String
        z=Regex.Split(",",NomeDelFile)
        Dim pos As Int
        pos=z(4).IndexOf("Name")
        NomeDelFile=z(4).SubString(pos+5) ' the name of the externalFile
    End If
End sub
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Maybe the analysis of the object cast to a string, like in my case, may be a way to distinguish the object type..
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Simply save a Boolean value:
True if from File.ListFiles
False if from ExternalStorage
(or vice versa, if you prefer)

In the CLV, you can use a Checkbox or B4XSwitch (crossplatform).
Or store the value you suggest as the Item.tag, I suppose. In general your suggestion is good. Thanks
 
Upvote 0

zed

Well-Known Member
Licensed User
use Is to test the type.
B4A allows you to test the type of an object using the Is operator.
Example:
If Value Is ExternalFile Then
    ' This is a file from ExternalStorage
Else If Value Is String Then
    ' It's a normal file (path)
End If

But be careful: ExternalFile is not a native B4A type.
ExternalStorage files are generally JavaObjects.

So you can do:
Example:
If Value Is JavaObject Then
    ' External file
Else If Value Is String Then
    ' Normal file
End If
untested code
 
Upvote 1
Solution
Top