Android Question create a map with a loop within

Robby Mamotte

Member
Licensed User
Longtime User
Hi
I am trying to create a map as per one of your examples but I need to loop in the create
where ID and Qty will be coming from a table
I have been looking all over for an example and just cannot seem to find one
I would appreciate if someone could help
Thank you

Dim jg As JSONGenerator
Dim m As Map = CreateMap("itemslist": CreateMap( _
over here loop with "itemID": ID,"Qty": Qnty, _
))
 

LucaMs

Expert
Licensed User
Longtime User
You can't do this with a single line of code.
Simply create the second Map and then add it to the first one.

B4X:
Do While ResultSet1.NextRow

    ID = ResultSet1.GetInt("ID")
    Qty = ResultSet1.GetDouble("Qty")
    mData.Put(ID, Qty)

Loop

mMainMap.Put("itemslist", mData)

ResultSet1.Close

(I didn't declare nor initialize the variables, it's just an example)
 
Upvote 0

Robby Mamotte

Member
Licensed User
Longtime User
Sorry i was trying to add the keys as well like this but the syntax wont allow it, is it possible?
mData.Put("itemID": ID,"Qty": Qnty)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Sorry i was trying to add the keys as well like this but the syntax wont allow it, is it possible?
mData.Put("itemID": ID,"Qty": Qnty)
No, and you don't need the "names", I think.

If you really want to give a name to those elements, create a custom type.

B4X:
Type tItem(itemID As Int, Qty As Double)

Hover over the name of your custom type and let the editor generate the function: CreatetItem (Sub).

Then:
B4X:
Do While ResultSet1.NextRow

    ID = ResultSet1.GetInt("ID")
    Qty = ResultSet1.GetDouble("Qty")

    Dim Item As tItem = CreatetItem(ID, Qry)
    mData.Put(ID, Item)

Loop

mMainMap.Put("itemslist", mData)

ResultSet1.Close
(but you will waste memory, as the ID value will be used twice, as a Map key and as a value)
 
Upvote 0

Robby Mamotte

Member
Licensed User
Longtime User
I thank you for your help, but it doesn't like CreatetItem
Please could you give me sample code as per your note above

Note: You could also create a List of tItems (of a custom type, as above).
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
but it doesn't like CreatetItem
This is created for you by the IDE, on the line Type tItem(...)

put your mouse on 'Type' and you will see the option to CreateItem.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4A_yfNcWBMn04.gif
 
Upvote 0

Robby Mamotte

Member
Licensed User
Longtime User
Thank you for all your help, but the answer i was really looking for was an example posted on one of the threads after extensive searching
I modified it to this to work for me, so Thanks again
B4X:
    Dim Items As List
    Items.Initialize
    Do While ResultSet1.NextRow
   
    ID = ResultSet1.GetInt("ID")
    Qty = ResultSet1.GetDouble("Qty")   
    Items.Add(CreateMap("itemid": ID, "qty": Qty))
   
    Loop

    Dim Data As Map = CreateMap("Version": 1.00, "ID": "abcdef","Items": Items)
    Dim jg As JSONGenerator
    jg.Initialize(Data)
    Dim JsonString As String = jg.ToPrettyString(4)
    EditText1.Text = JsonString
 
Last edited:
Upvote 0
Top