Android Question map to List and reverse

FrankDev

Active Member
Licensed User
Longtime User
Probably a beginner problem. But I'm just not clear. When I write data into a map and then add it to a list Why do not I get it out the other way around?!? regards

B4X:
 dim liste as list

 'put some data into a list

 Dim m As Map
 m.Initialize
 m.Put("id_user", From)
 m.Put("message",Message)
 m.Put("dtime", Zeit)
 liste.Add(m)


 
 'back from list to variable

 Dim m As Map = liste.Get(1)
 Dim msg As String = m.Get("message")
 

udg

Expert
Licensed User
Longtime User
Dim m AsMap = liste.Get(1)
Index should be zero since you add just one item
 
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
no.. the List contains hundred of items

is this realy the right syntax ?
 

Attachments

  • Screenshot_20170606-105803.png
    69.9 KB · Views: 269
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
Apart from the index to the list (which starts at zero) your code should work:

B4X:
    Dim From,Message,Zeit As String
       
    Zeit=DateTime.Now
    From= "abc@example.com"
    Message="This is a message.More ...    "

    Dim liste As List
     liste.Initialize
    'put some data into a list

    Dim m As Map
   
    m.Initialize
    m.Put("id_user",From)
    m.Put("message",Message)
    m.Put("dtime", Zeit)
    Log("Map: " & m)
    liste.Add(m)
   
    Log(liste)
    'back from list to variable
    Dim m As Map = liste.Get(0) 'List index starts at zero
    Log("Retrieved message"  & m)
    Dim msg As String = m.Get("message")
   
    Log("Msg: " & msg)

This gives:

 
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
hi Derek,

thanks for help.. i found MY mistake.
Thought that the list is already initialized. but it wasn't


A small mistake that cost me many hours.
I thought I had a wrong syntax

much thanks.
 
Reactions: eps
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
hi Derek,

thanks for help.. i found MY mistake.
Thought that the list is already initialized. but it wasn't


A small mistake that cost me many hours.
I thought I had a wrong syntax

much thanks.

I am surprised that you did not spot the lack of initialisation of the list object. If you were running in Debug mode, your first statement to add an item to the list would have shown up this error in the Debug log:

Error occurred on line: 65 (Main)
java.lang.RuntimeException: Object should first be initialized (List).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
...etc

Anyway the moral is, always use debug mode to start with and always look at the log!

B4A is a great development environment and also has a great support community!

Good luck!

Derek
 
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
have never tried to use the debugmode because I always test on the smartphone.

I did not know that worked there.

Well that we talked about it

From mistakes one becomes wise

Frank
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…