如何将dataSnapshot放入HashMap并迭代Firebase Android

时间:2022-10-29 12:52:15

I am trying to turn a DataSnapshot object to HashMap then iterate it for accessing latitude longitude and avatar values for each player. Because after this i will use those values to render markers on map.

我正在尝试将DataSnapshot对象转换为HashMap,然后迭代它以访问每个玩家的纬度经度和头像值。因为在此之后我将使用这些值在地图上渲染标记。

db

Another question is is it possible to exclude users itself from the query ? I mean is it possible to write mydb.child("map").child("players").notequalto(username).addValueEventList... Or do i have to exclude it with code after pulling the data?

另一个问题是可以从查询中排除用户自己吗?我的意思是可以编写mydb.child(“map”)。child(“players”)。notequalto(username).addValueEventList ...或者我必须在提取数据后用代码将其排除?

    mydb.child("map").child("players").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                HashMap<String,Object> players= new HashMap();
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                players.put(childSnapshot.getKey(), childSnapshot.getValue());
            }
                    Log.d("players",dataSnapshot.getValue().toString());





            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

output:

 D/players: {lifesuxtr={lat=40.8901741, long=29.3772963, avatar =petyr}, lastpeony={lat=40.89, long=29.37, avatar=dino}}

1 个解决方案

#1


1  

You don't need to put the dataSnapshot object into a HashMap. It already returns a Map. So the following line of code is useless.

您不需要将dataSnapshot对象放入HashMap。它已经返回了一张地图。所以下面的代码行是没用的。

players.put(childSnapshot.getKey(), childSnapshot.getValue());

Assuming that the map node is a direct child of your Firebase database root, to get those values, please the following code:

假设地图节点是Firebase数据库根目录的直接子节点,要获取这些值,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference playersRef = rootRef.child("map").child("players");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Map<Double, Double> players = new HashMap<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            double lat = ds.child("lat").getValue(Double.class);
            double long = ds.child("long").getValue(Double.class);
            Log.d("TAG", lat + " / " + long);
            players.put(lat, long);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
playersRef.addListenerForSingleValueEvent(eventListener);

The output will be:

输出将是:

40.89 / 29.37
40.89 / 29.37

Having there values, you can now add your markers. And answering to your question, no, you cannot use negation in a query. There is no notEqualTo() method in Firebase.

拥有值,您现在可以添加标记。回答你的问题,不,你不能在查询中使用否定。 Firebase中没有notEqualTo()方法。

#1


1  

You don't need to put the dataSnapshot object into a HashMap. It already returns a Map. So the following line of code is useless.

您不需要将dataSnapshot对象放入HashMap。它已经返回了一张地图。所以下面的代码行是没用的。

players.put(childSnapshot.getKey(), childSnapshot.getValue());

Assuming that the map node is a direct child of your Firebase database root, to get those values, please the following code:

假设地图节点是Firebase数据库根目录的直接子节点,要获取这些值,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference playersRef = rootRef.child("map").child("players");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Map<Double, Double> players = new HashMap<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            double lat = ds.child("lat").getValue(Double.class);
            double long = ds.child("long").getValue(Double.class);
            Log.d("TAG", lat + " / " + long);
            players.put(lat, long);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
playersRef.addListenerForSingleValueEvent(eventListener);

The output will be:

输出将是:

40.89 / 29.37
40.89 / 29.37

Having there values, you can now add your markers. And answering to your question, no, you cannot use negation in a query. There is no notEqualTo() method in Firebase.

拥有值,您现在可以添加标记。回答你的问题,不,你不能在查询中使用否定。 Firebase中没有notEqualTo()方法。