为什么我在Firebase-RealDatabase上得到ArrayList而不是HashMap? [英] Why I am getting an ArrayList instead a HashMap on Firebase-RealDatabase?

查看:78
本文介绍了为什么我在Firebase-RealDatabase上得到ArrayList而不是HashMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库:

    {
    "Shops": {
        "Title": {
            "1": "Footlocker",
            "2": "Nike Store",
            "3": "Adidas Store"
        },
        "Lat": {
            "1": "123",
            "2": "123",
            "3": "123"
        },
        "Lon": {
            "1": "123",
            "2": "123",
            "3": "123"
        }
    }
}

数字"1","2"和"3"代表商店.例如,"1"商店的标题为"Footlocker",纬度为"123"(可能应该是数字,但这不是我的问题).

The numbers "1","2" and "3" represent a shop. For example "1" shop has "Footlocker" for title and "123" latitude (probably this should be a number, but that's not my problem).

我的目标是在Hashmap<String,String>中获取所有标题(键为"1" ...值"Footlocker"等...)

My goal is to get all the titles in a Hashmap<String,String> (key would be "1"... value "Footlocker" etc...)

所以我创建了对数据库中标题"键的引用并添加了侦听器,

So I create a reference to "Title" key in database and add the listener,

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Shops").child("Title");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                HashMap<String,String> map = (HashMap<String,String>) dataSnapshot.getValue();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

您可以想象上面的代码将引发异常,因为ArrayList无法强制转换为HashMap.

As you can imagine the above code throws an exception because an ArrayList cannot be cast to a HashMap.

我的问题是,我是否应该获取一个HashMap,而不是将数字作为键,将标题作为值?

推荐答案

您已将数据库"显示为JSON文档.

You've shown your "database" as a JSON document.

如果您使用JSON解析器进行解析,可以肯定会期望有一个TreeMap,但是用Firebase的术语来说,我认为您已经将元素的索引"与您认为应该是钥匙".

If you parsed that using a JSON parser, sure, you might expect a TreeMap, to be exact, but in Firebase's terms, I think you've confused the "index" of the elements with what you think should be a "key".

因此,您得到的是列表,而不是地图.没什么大不了的.您仍然可以遍历元素

So, you get a list, not a Map. No big deal, really. You can still iterate over the elements

如果您想要更好的结构,我建议

If you want a (somewhat) better structure, I would suggest

Stores 
    Footlocker
        Lat
        Lon
    Nike
       ... 
    Adidas
       ... 

或者作为弗兰克评论,使用Firebase可以生成的自然键顺序.

Or as Frank comments, use the natural key ordering that Firebase can generate.

Stores 
    <id_0>
        name: "Footlocker"
        Lat: 0.00
        Lon: 0.00
    <id_1>
        name: "Nike"
        ...
    <id_2> 
        name: "Adidas"
        ... 


附加点.不要使用HashMap,使用对象来表示数据


Additional point. Don't use a HashMap, use objects to represent the data

public class Store {
    String name;
    Double lat, lon;

    public Store() {
        // Required empty-constructor
    }

    // getters & setters...
}

这篇关于为什么我在Firebase-RealDatabase上得到ArrayList而不是HashMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆