Firebase实时数据库返回空值

时间:2020-01-31 13:08:44

标签: android firebase firebase-realtime-database

dbRef1 = database.getReference("/parkingLocation/marinParking/max");
dbRef2 = database.getReference("/parkingLocation/marinParking/curr");

...

ValueEventListener changeListener = new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        Log.d(TAG,"In change listener");
        String firstValue = dataSnapshot.child(currUser.getUid()).child("parkingLocation").child("marinParking").child("max").getValue(String.class);
        Log.d(TAG,"The value of max is " + firstValue);
        String secondValue = dataSnapshot.child(currUser.getUid()).child("parkingLocation").child("marinParking").child("curr").getValue(String.class);
        Log.d(TAG,"The value of current is" + secondValue);
        Toast.makeText(MarinParkingActivity.this,firstValue,Toast.LENGTH_SHORT).show();
        maxTextView.setText(firstValue);
        currTextView.setText(secondValue);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        notifyUser("Database Error " + databaseError.toException());
    }
};

我正在尝试从Firebase实时数据库检索数据。我正在使用上面的代码从数据库中获取数据,但是我收到的是空值。

我收到以下日志

2020-01-31 18:28:29.831 28878-28878 / com.example.parkings D / MarinParking:在更改侦听器中 2020-01-31 18:28:29.832 28878-28878 / com.example.parkings D / MarinParking:max的值为null 2020-01-31 18:28:29.832 28878-28878 / com.example.parkings D / MarinParking:当前不为空的值 2020-01-31 18:28:30.256 28878-28878 / com.example.parkings D / MarinParking:在更改监听器中 2020-01-31 18:28:30.258 28878-28878 / com.example.parkings D / MarinParking:max的值为空 2020-01-31 18:28:30.259 28878-28878 / com.example.parkings D / MarinParking:当前值为null

这是我的数据库:

And this is my database

2 个答案:

答案 0 :(得分:1)

更改此:

getattr

对此:

        String firstValue = dataSnapshot.child(currUser.getUid()).child("parkingLocation").child("marinParking").child("max").getValue(String.class);

        String secondValue = dataSnapshot.child(currUser.getUid()).child("parkingLocation").child("marinParking").child("curr").getValue(String.class);

您的数据库中没有 String firstValue = dataSnapshot.child("parkingLocation").child("marinParking").child("max").getValue(String.class); String secondValue = dataSnapshot.child("parkingLocation").child("marinParking").child("curr").getValue(String.class);

答案 1 :(得分:0)

更改此

       String firstValue = dataSnapshot.child(currUser.getUid()).child("parkingLocation").child("marinParking").child("max").getValue(String.class);
       String secondValue = dataSnapshot.child(currUser.getUid()).child("parkingLocation").child("marinParking").child("curr").getValue(String.class);

    String firstValue = dataSnapshot.getValue(String.class);
    String secondValue = dataSnapshot.getValue(String.class);

因为您已经在此处提供了位置路径。

        dbRef1 = database.getReference("/parkingLocation/marinParking/max");
        dbRef2 = database.getReference("/parkingLocation/marinParking/curr");

根据您当前的代码(在评论中发布),对每个dbRef使用单独的ValueEventListener。您无法在单个ValueEventListener中获得两个结果。

相关问题