如何在Android中迭代Firebase结构?

时间:2016-06-14 19:02:33

标签: java android json firebase

我想创建一个遍历firebase结构的函数,如下所示:

{
   "users":{
      "7e122736-2dd4-4770-a360-a0e7cbe41a43":{
         "currentLatitude":46.6598714,
         "currentLongitude":23.5637339,
         "displayName":"gjsj2",
         "email":"2@vlad.com",
         "password":"123"
      },
      "a09e7e1d-ad3a-4d21-b0ba-069e0999bb93":{
         "currentLatitude":47.6599014,
         "currentLongitude":23.5636797,
         "displayName":"gjsj",
         "email":"1@vlad.com",
         "password":"123"
      },
      "abc29286-fd6d-4088-95da-759828b5835d":{
         "currentLatitude":50.6599043,
         "currentLongitude":23525.5637188,
         "displayName":"gjsj3",
         "email":"3@vlad.com",
         "password":"123"
      }
   }
}

我想创建一个功能,该功能可以获取每个唯一用户ID的子项,并使用其坐标在Google地图上创建标记。这是我到目前为止所得到的,但它似乎没有起作用:

public void onChildChanged(DataSnapshot snapshot, String s) {
    for (DataSnapshot userSnapshot : snapshot.getChildren()) {
        for (DataSnapshot uniqueUserSnapshot : userSnapshot.getChildren()) {
            Users currentUser = uniqueUserSnapshot.getValue(Users.class);

            MarkerOptions options = new MarkerOptions()
                    .title(currentUser.getEmail())
                    .icon(BitmapDescriptorFactory.defaultMarker(getRandomNumberInRange(0, 360)))
                    .position(new LatLng(currentUser.getCurrentLatitude(), currentUser.getCurrentLongitude()));
            mMap.addMarker(options);
            Toast.makeText(MainActivity.this, "fsafasfasfas", Toast.LENGTH_SHORT).show();
        }
    }
}

这是我的用户POJO:

package com.licenta.vladut.mmap;

public class Users {
    String email;
    String displayName;
    String password;
    double currentLatitude;
    double currentLongitude;


    public Users() {
    }

    public Users(String email, String displayName, String password, double currentLongitude, double currentLatitude) {
        this.email = email;
        this.displayName = displayName;
        this.password = password;
        this.currentLongitude = currentLongitude;
        this.currentLatitude = currentLatitude;
    }

    public Users(String email, String displayName, String password) {
        this.email = email;
        this.displayName = displayName;
        this.password = password;
    }

    public String getDisplayName() {
        return displayName;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public double getCurrentLongitude() {
        return currentLongitude;
    }

    public double getCurrentLatitude() {
        return currentLatitude;
    }
}

1 个答案:

答案 0 :(得分:5)

如果我正确理解了这个问题,那么你实际上接近你想做的事情。引用数据库子用户,然后添加ValueEventListener:

Firebase userRef = new Firebase(/*url*/);
userRef = userRef.child("users");
userRef.addValueEventListener(new ValueEventListener(){
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot uniqueUserSnapshot : dataSnapshot.getChildren()) {
            ...
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

ValueEventListener一个接一个地返回被调用的节点,而不是子节点。