The hashtable elements' order is changed after JSONParser in multilist under Android device only

92 views
Skip to first unread message

Pai Peng

unread,
Jul 22, 2013, 10:54:12 AM7/22/13
to codenameone...@googlegroups.com
Hello CN1,

now I have noticed one problem on my Samsung device with the hashtable.

The app receives JSON object from server. I use JSONParser to get a Hashtable object. Then using the following code to create a vector:

Enumeration enumeration = trashHotspots.elements();
                   while (enumeration.hasMoreElements()) {
                       Hashtable temp = (Hashtable)(enumeration.nextElement());
                       temp.put("abc", temp.get("abc"));
                       
                       owner.downloadImage((String)temp.get("photo_id"), v.size());
                       v.addElement(temp);
                   }

The vector is used for displaying in a Multilist.
The server sends the elements with desc order in JSON. But on Android device, the order (by id) in Multilist is changed: like 51->49->50->47->48->45....

But by testing on simulator and iPhone, the order is correct:51->50->49->48->47->46...

Do I make any mistake here?


Regards,

Pai


Steve Hannah

unread,
Jul 22, 2013, 11:10:30 AM7/22/13
to codenameone...@googlegroups.com
Hash tables have no order and JSON parsers do not preserve order of keys when parsing objects.  If order is important change the structure to an array of objects rather than just an object.

Steve
--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/878da70a-b3d3-4151-a004-191642d9daf0%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Steve Hannah
Web Lite Solutions Corp.

Pai Peng

unread,
Jul 23, 2013, 4:18:56 AM7/23/13
to codenameone...@googlegroups.com
Hello Steve,

Thanks for your help.

I have to sort them in Vector. And it works fine now also on Android.

Regards,

Pai


On Monday, July 22, 2013 5:10:30 PM UTC+2, shannah wrote:
Hash tables have no order and JSON parsers do not preserve order of keys when parsing objects.  If order is important change the structure to an array of objects rather than just an object.

Steve

On Monday, July 22, 2013, Pai Peng wrote:
Hello CN1,

now I have noticed one problem on my Samsung device with the hashtable.

The app receives JSON object from server. I use JSONParser to get a Hashtable object. Then using the following code to create a vector:

Enumeration enumeration = trashHotspots.elements();
                   while (enumeration.hasMoreElements()) {
                       Hashtable temp = (Hashtable)(enumeration.nextElement());
                       temp.put("abc", temp.get("abc"));
                       
                       owner.downloadImage((String)temp.get("photo_id"), v.size());
                       v.addElement(temp);
                   }

The vector is used for displaying in a Multilist.
The server sends the elements with desc order in JSON. But on Android device, the order (by id) in Multilist is changed: like 51->49->50->47->48->45....

But by testing on simulator and iPhone, the order is correct:51->50->49->48->47->46...

Do I make any mistake here?


Regards,

Pai


--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages