Android Question Order in Map

iCAB

Well-Known Member
Licensed User
Longtime User
Hi There

1. Say we have a map, that holds a list of objects
2. I read the map, and display the items in a scroll list in the same order read from the map
3. if I provide the user with the tools to edit an item in the scroll list and save the contents of the updated item back to the map using map.put(Key,value)

Can I assume that the updated item holds the exact same position in the map, as it did before "put"?
in other words, would the order of the items displayed in the original scroll list correspond to the order of the items in the map after the "put" operation?

Thanks in advance?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Have you tested this to see if it works the way you want?

I'm inclined to say that you shouldn't rely on the Map preserving order. Accessing Map elements by index is a legacy method from before B4X had the ForEach construct. If you want to maintain the order of your ScrollView, I recommend you get all the Keys in the Map, put them in a standard List, sort that List (alphabetically, numerically, however you like) and then iterate through that sorted List to get the Values. This way, changing a Value won't alter that Value's Key's position in the Key List.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
My currently implementation is more or less similar to your suggestion.

But the reason, I asked the question is this:
I tested the following in a loop several times.

Read Map
Create Scroll list
Do
update an element using map.put
read the Map into a temp object
compare order with original map
Loop

it never failed.
If I guarantee this, it will simplify the code and make a huge difference in terms of the application being more responsive to user input

Thanks for your reply
 
Upvote 0
Top