Android Question Got my info INTO a custom type now what??

Adrian Smith

New Member
Hi, I have been using maps, but it got messy, So I took a look and a custom type seemed to be my solution.

I can read an online JSON, and load the information into my custom type. I was expecting reading the data back to be simple! I have searched the forum for tutorials, even to get the data into a type was copied from a youtube video (ETP Objects & custom types here).

the type is simply

B4X:
DownloadablesList.Add(CreateApp(quot.Get("app_name"),quot.Get("image_url"),quot.Get("app_url"),quot.Get("app_description"),quot.Get("app_id")))

and currently has 39 items in it, if I log whilst adding the data I can see the data has gone into the type

B4X:
Log(DownloadablesList.Get(DownloadablesList.Size-1))

I was hoping this was going to solve my sorting problem but I cant even read a field??

So how do I get the data back into usable form? eg

Imaginary code!

Where downloadableslist.app_name contains "123 app"

or

Where downloadableslist.app_description contains "a great app"

Help! please
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
It sounds like you have the wrong mental model of how custom types work, especially if you see them as an alternative to maps. Send a bigger sample of code, at least a complete subroutine, and someone might be able to help you out.
 
Upvote 0

Adrian Smith

New Member
It sounds like you have the wrong mental model of how custom types work, especially if you see them as an alternative to maps. Send a bigger sample of code, at least a complete subroutine, and someone might be able to help you out.

Basically I believed a custom type would be similar to a map? as in it has all the data? and it has field headings? I was expecting to be able to search by field or globally, change the order of it? or the contents?

If it doesn't work like that! then I will go back to maps. (I am self taught so I am just feeling my way around)

Thanks.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
You are on the right lines, but a custom type and a Map are quite different. To use database terminology, a custom type is equivalent to a record and the variables within it are fields. For example ...
B4X:
    Type track (title As String, artist As String, album As String, duration as int)
If you add several of these objects to a List then the list is equivalent to a database table. In B4X you can easily sort a list (table) based on a field value ...
B4X:
    Dim tracklist As List
    tracklist.Add(aTrack)                       ' "aTrack" is a track object
    tracklist.Add(...)
    tracklist.Add(...)
    ...  ...
    tracklist.SortType("artist", True)
If you want to retrieve records from a List then you can use something like this ...
B4X:
Sub selectArtist(name As String) As List
    Dim result As List
    result.Initialize
    For Each t As track In tracklist
        If (t.artist = name) Then result.Add(t)
    Next
    Return result                            ' List of track objects with "artist = name"
End Sub
A Map, in this context, is a keyed table. The values would be "track" objects. The keys are often one of the field values, but they need to be unique. That might not be possible in the current example, but maybe ...
B4X:
    Dim library As Map
    Dim t As track
    t.title = "Mona"
    t.artist = "Rolling Stones"
    t.album = "Now!"
    t.duration = 235  
    library.Put(t.title, t)
    . . . .
    . . . .
    t = library.Get("Mona")
Now you can retrieve complete track data based on a track title, like a simple database query. It is faster than scanning a list.

You are getting your data from a JSON file. I haven't used JSON in B4A but a JSONParser will directly generate a Map of key:value pairs. That might be helpful, although I think that I would organise the data into larger objects as you seem to be doing.

I hope that I have helped. Sorry if I have explained some stuff that you already know. Ask again if I have not been clear.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…