从Firebase中的嵌套保存数据中检索数据

时间:2019-07-15 08:19:00

标签: java android firebase firebase-realtime-database

我有这样的结构:

enter image description here

我需要从“ OnlineClients”获取数据。我的尝试:

currentDriverId = mFireBaseAuth.getCurrentUser().getUid();
mDataBase = FirebaseDatabase.getInstance().getReference().child("Users").child("Driver").child(currentDriverId).child("OnlineClients");
final ArrayList<Student> users = new ArrayList<>();
        ValueEventListener postListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                    Student student  = data.getValue(Student.class);
                    users.add(student);
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Getting Post failed, log a message
            }
        };
        mDataBase.addListenerForSingleValueEvent(postListener);

有错误:

  

com.google.firebase.database.DatabaseException:转换失败   String的java.lang.Long类型的值

Student.Class:

public class Student {
    private String FirstName;
    private String LastName;
    private String StudentSchool;
    private String StudentState;
    private String StudentStreet;
    private String StudentStreetNumber;


    public Student(){
    }

    public String getFirstName() {
        return FirstName;
    }

    public void setFirstName(String firstName) {
        FirstName = firstName;
    }

    public String getLastName() {
        return LastName;
    }

    public void setLastName(String lastName) {
        LastName = lastName;
    }

    public String getStudentSchool() {
        return StudentSchool;
    }

    public void setStudentSchool(String studentSchool) {
        StudentSchool = studentSchool;
    }

    public String getStudentState() {
        return StudentState;
    }

    public void setStudentState(String studentState) {
        StudentState = studentState;
    }

    public String getStudentStreet() {
        return StudentStreet;
    }

    public void setStudentStreet(String studentStreet) {
        StudentStreet = studentStreet;
    }

    public String getStudentStreetNumber() {
        return StudentStreetNumber;
    }

    public void setStudentStreetNumber(String studentStreetNumber) {
        StudentStreetNumber = studentStreetNumber;
    }
}

已经尝试过:how to retrieve all the data within nested firebase database和类似的尝试,但没有成功。

注意:我只需要从当前登录的用户中检索“ OnlineClients”(这就是我仅使用currentDriverId的原因)。

2 个答案:

答案 0 :(得分:1)

更改此:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
  for (DataSnapshot data : dataSnapshot.getChildren()) {
      Student student  = data.getValue(Student.class);
      users.add(student);
   }

}

对此:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
  Student student  = dataSnapshot.getValue(Student.class);
  users.add(student);
}

删除for循环,因为在循环时,您将以String的形式检索值。另外,请确保在使用setValue()时要添加类型Student的对象作为参数。

检索值的另一种方法是执行以下操作:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
 for (DataSnapshot data : dataSnapshot.getChildren()) {
    String firstName = data.child("FirstName").getValue(String.class);
    String lastName  = data.child("LastName").getValue(String.class);
     }
}

答案 1 :(得分:0)

代码中的问题在于,您的StudentStreetNumber字段是在类型为Student的{​​{1}}类中声明的,而在数据库中则是一个数字(长整数)。要解决此问题,只需将您的媒体资源类型更改为String

long

代码中的另一个问题是吸气剂。同样也要解决此问题,请从以下帖子中查看我的答案: