Android Question Adding maps to a list iteratively

jamesnz

Active Member
Licensed User
Longtime User
I'm populating an object called books in a loop
B4X:
Dim b1 as book
b1.title="blah"
b1.author="C.Dickens"
etc
I'm then adding the book objects to a map
B4X:
mapofbooks.Put("book"&bookcounter,b1)
I do this in a loop, ( I clear the object by creating a fresh book object and set the old one to the new one)
B4X:
Dim bnew as book
b1=bnew
I then repopulate the book object
B4X:
b1.title="new title"
etc
I add that new object to the same map and when I look at the map in the debugger everything is fine, I can see a map containing all the objects
no problem.

I then add the map of objects to a list
B4X:
Main.listofmaps.Add(Main.mapofbooks)
so now I intend to add another different series of books
but when I clear the map of objects
B4X:
Main.mapofbooks.Clear
the list now just contains empty maps
and if I don't clear it, all of the maps in the list are identical to each other

I'm guessing this is occurring because maps store data byreference rather than byvalue ?
Which is fine but how do I deal with this ?
 

DonManfred

Expert
Licensed User
Longtime User
Please upload a small example project which shows the issue.

B4X:
Dim b1 as book
b1.initialize ' Don´t forget it
b1.title="blah"
b1.author="C.Dickens"
' now add the Book to the Map

I clear the object by creating a fresh book object and set the old one to the new on
This sounds wrong.
B4X:
Dim b1 as book
b1.initialize ' Don´t forget it
b1.title="blah" ' value in your loop
b1.author="blub" ' value in your loop
' now add the Book to the Map
 
Upvote 0

jamesnz

Active Member
Licensed User
Longtime User
Ive always cleared an object by creating a new one and setting that to the old one ....If I don't eg, in the addbooksfiction, note what happens to the title and id, id is always 50 and fiction_title is always 10 because the last value of j was 10

in the addbooksnonfiction I create a new object each time through the loop and assign this to the old object, you can see that the map stores the value of i properly

If you comment out line 60 and 61 you'll see the difference



But I digress, this isnt really the problem I'm having

The problem is when the mapofbooks is added to the listofmaps
the list of maps contains identical maps

If you run line 72 which I would have thought would clear the first map, so the same data isnt added twice, then what you get in the list is 2 identical maps ( same data)

If you comment out line 72 you get both maps but added twice ( a total of 4 )

Perhaps its just getting late but boy, I am thoroughly confused

What I want to achieve is a a list that holds two maps, one map with the nonfictionbooks in it and another map with the fiction books.
 

Attachments

  • BooksExamplezipped.zip
    33.3 KB · Views: 264
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
--------- beginning of system
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
____________MAP OF BOOKS AFTER FIRST 10_______
(MyMap) {booknf1=[author=C.Dickens, id=2, main=null
, starter=null, title=non-fictiontitle1], booknf2=[author=C.Dickens, id=4, main=null
, starter=null, title=non-fictiontitle2], booknf3=[author=C.Dickens, id=6, main=null
, starter=null, title=non-fictiontitle3], booknf4=[author=C.Dickens, id=8, main=null
, starter=null, title=non-fictiontitle4], booknf5=[author=C.Dickens, id=10, main=null
, starter=null, title=non-fictiontitle5], booknf6=[author=C.Dickens, id=12, main=null
, starter=null, title=non-fictiontitle6], booknf7=[author=C.Dickens, id=14, main=null
, starter=null, title=non-fictiontitle7], booknf8=[author=C.Dickens, id=16, main=null
, starter=null, title=non-fictiontitle8], booknf9=[author=C.Dickens, id=18, main=null
, starter=null, title=non-fictiontitle9], booknf10=[author=C.Dickens, id=20, main=null
, starter=null, title=non-fictiontitle10]}
______________________________________________
_____MAPOFBOOKS (after 2nd loop)____________________________
(MyMap) {bookf'1=[author=D.Smith, id=5, main=null
, starter=null, title=fiction_title1], bookf'2=[author=D.Smith, id=10, main=null
, starter=null, title=fiction_title2], bookf'3=[author=D.Smith, id=15, main=null
, starter=null, title=fiction_title3], bookf'4=[author=D.Smith, id=20, main=null
, starter=null, title=fiction_title4], bookf'5=[author=D.Smith, id=25, main=null
, starter=null, title=fiction_title5], bookf'6=[author=D.Smith, id=30, main=null
, starter=null, title=fiction_title6], bookf'7=[author=D.Smith, id=35, main=null
, starter=null, title=fiction_title7], bookf'8=[author=D.Smith, id=40, main=null
, starter=null, title=fiction_title8], bookf'9=[author=D.Smith, id=45, main=null
, starter=null, title=fiction_title9], bookf'10=[author=D.Smith, id=50, main=null
, starter=null, title=fiction_title10]}
_______________HERE IS LIST -should be BOTH MAPS__________________________________
(ArrayList) [{booknf1=[author=C.Dickens, id=2, main=null
, starter=null, title=non-fictiontitle1], booknf2=[author=C.Dickens, id=4, main=null
, starter=null, title=non-fictiontitle2], booknf3=[author=C.Dickens, id=6, main=null
, starter=null, title=non-fictiontitle3], booknf4=[author=C.Dickens, id=8, main=null
, starter=null, title=non-fictiontitle4], booknf5=[author=C.Dickens, id=10, main=null
, starter=null, title=non-fictiontitle5], booknf6=[author=C.Dickens, id=12, main=null
, starter=null, title=non-fictiontitle6], booknf7=[author=C.Dickens, id=14, main=null
, starter=null, title=non-fictiontitle7], booknf8=[author=C.Dickens, id=16, main=null
, starter=null, title=non-fictiontitle8], booknf9=[author=C.Dickens, id=18, main=null
, starter=null, title=non-fictiontitle9], booknf10=[author=C.Dickens, id=20, main=null
, starter=null, title=non-fictiontitle10]}, {bookf'1=[author=D.Smith, id=5, main=null
, starter=null, title=fiction_title1], bookf'2=[author=D.Smith, id=10, main=null
, starter=null, title=fiction_title2], bookf'3=[author=D.Smith, id=15, main=null
, starter=null, title=fiction_title3], bookf'4=[author=D.Smith, id=20, main=null
, starter=null, title=fiction_title4], bookf'5=[author=D.Smith, id=25, main=null
, starter=null, title=fiction_title5], bookf'6=[author=D.Smith, id=30, main=null
, starter=null, title=fiction_title6], bookf'7=[author=D.Smith, id=35, main=null
, starter=null, title=fiction_title7], bookf'8=[author=D.Smith, id=40, main=null
, starter=null, title=fiction_title8], bookf'9=[author=D.Smith, id=45, main=null
, starter=null, title=fiction_title9], bookf'10=[author=D.Smith, id=50, main=null
, starter=null, title=fiction_title10]}]
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
____________MAP OF BOOKS AFTER FIRST 10_______
(MyMap) {booknf1=[author=C.Dickens, id=2, main=null
, starter=null, title=non-fictiontitle1], booknf2=[author=C.Dickens, id=4, main=null
, starter=null, title=non-fictiontitle2], booknf3=[author=C.Dickens, id=6, main=null
, starter=null, title=non-fictiontitle3], booknf4=[author=C.Dickens, id=8, main=null
, starter=null, title=non-fictiontitle4], booknf5=[author=C.Dickens, id=10, main=null
, starter=null, title=non-fictiontitle5], booknf6=[author=C.Dickens, id=12, main=null
, starter=null, title=non-fictiontitle6], booknf7=[author=C.Dickens, id=14, main=null
, starter=null, title=non-fictiontitle7], booknf8=[author=C.Dickens, id=16, main=null
, starter=null, title=non-fictiontitle8], booknf9=[author=C.Dickens, id=18, main=null
, starter=null, title=non-fictiontitle9], booknf10=[author=C.Dickens, id=20, main=null
, starter=null, title=non-fictiontitle10]}
______________________________________________
_____MAPOFBOOKS (after 2nd loop)____________________________
(MyMap) {bookf'1=[author=D.Smith, id=5, main=null
, starter=null, title=fiction_title1], bookf'2=[author=D.Smith, id=10, main=null
, starter=null, title=fiction_title2], bookf'3=[author=D.Smith, id=15, main=null
, starter=null, title=fiction_title3], bookf'4=[author=D.Smith, id=20, main=null
, starter=null, title=fiction_title4], bookf'5=[author=D.Smith, id=25, main=null
, starter=null, title=fiction_title5], bookf'6=[author=D.Smith, id=30, main=null
, starter=null, title=fiction_title6], bookf'7=[author=D.Smith, id=35, main=null
, starter=null, title=fiction_title7], bookf'8=[author=D.Smith, id=40, main=null
, starter=null, title=fiction_title8], bookf'9=[author=D.Smith, id=45, main=null
, starter=null, title=fiction_title9], bookf'10=[author=D.Smith, id=50, main=null
, starter=null, title=fiction_title10]}
_______________HERE IS LIST -should be BOTH MAPS__________________________________
(ArrayList) [{booknf1=[author=C.Dickens, id=2, main=null
, starter=null, title=non-fictiontitle1], booknf2=[author=C.Dickens, id=4, main=null
, starter=null, title=non-fictiontitle2], booknf3=[author=C.Dickens, id=6, main=null
, starter=null, title=non-fictiontitle3], booknf4=[author=C.Dickens, id=8, main=null
, starter=null, title=non-fictiontitle4], booknf5=[author=C.Dickens, id=10, main=null
, starter=null, title=non-fictiontitle5], booknf6=[author=C.Dickens, id=12, main=null
, starter=null, title=non-fictiontitle6], booknf7=[author=C.Dickens, id=14, main=null
, starter=null, title=non-fictiontitle7], booknf8=[author=C.Dickens, id=16, main=null
, starter=null, title=non-fictiontitle8], booknf9=[author=C.Dickens, id=18, main=null
, starter=null, title=non-fictiontitle9], booknf10=[author=C.Dickens, id=20, main=null
, starter=null, title=non-fictiontitle10]}, {bookf'1=[author=D.Smith, id=5, main=null
, starter=null, title=fiction_title1], bookf'2=[author=D.Smith, id=10, main=null
, starter=null, title=fiction_title2], bookf'3=[author=D.Smith, id=15, main=null
, starter=null, title=fiction_title3], bookf'4=[author=D.Smith, id=20, main=null
, starter=null, title=fiction_title4], bookf'5=[author=D.Smith, id=25, main=null
, starter=null, title=fiction_title5], bookf'6=[author=D.Smith, id=30, main=null
, starter=null, title=fiction_title6], bookf'7=[author=D.Smith, id=35, main=null
, starter=null, title=fiction_title7], bookf'8=[author=D.Smith, id=40, main=null
, starter=null, title=fiction_title8], bookf'9=[author=D.Smith, id=45, main=null
, starter=null, title=fiction_title9], bookf'10=[author=D.Smith, id=50, main=null
, starter=null, title=fiction_title10]}]
 

Attachments

  • BooksExamplezipped.zip
    7.4 KB · Views: 249
Upvote 0

jamesnz

Active Member
Licensed User
Longtime User
So, what if that book data isn't coming from inside a loop and instead it's coming from a XML return via web.
The XML parser fires it's end element event many times as it parses the nested parts of the XML. yet I'd need the objects and map to persist from one event firing to the next.
 
Upvote 0

jamesnz

Active Member
Licensed User
Longtime User
I'm still on 5.5 but Ill remedy that shortly
Do you know if xml2map can handle large docs with nested namespaces?
My xml return is 3.5mb
 
Upvote 0
Top