无法从Firebase数据库获取数据 - 它返回空值

时间:2018-05-02 06:46:50

标签: android database firebase null return

我正在尝试从Firebase检索数据:

 DatabaseReference customerRef = FirebaseDatabase.getInstance().getReference("Drivers").child(driverId).child("customerRideId");
 Log.d("TAG_SiX",customerRef.toString());

 customerRef.addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
        // retriveCustomerID(dataSnapshot);
         Log.d("TAG_eight",dataSnapshot.toString());
         Map<String,String> map = 
           (Map<String,String>)dataSnapshot.getValue();

     }

     @Override
     public void onCancelled(DatabaseError databaseError) {

     }
 });

这是logcat值

D/TAg_five: DataSnapshot { key = customerRideId, value = null }

05

以下是Firebase数据结构,我希望customerRideId的值在logcat中显示为null,而customerRideId在DriverID中。

Drivers
    PyltxOLXcWfZf6itgJEMWC4ylDu2
           driverEmail: "nishant@gmail.com"
           driverName: "Nishant"
    customerRideId: "Sqpb6RFNhMOSiXeCrYj83aeeG7r2"

我想检索customerRideId的值,但是在logcat中它显示空值,请帮我解决。

here is the database screenshot

DataBase Screen shot here

3 个答案:

答案 0 :(得分:0)

确保在gradle中添加了适当的依赖项,并且firebase DB中有一些数据 如果所有这些都很好,请尝试:

private FirebaseDatabase database;

在你的方法中:

database=FirebaseDatabase.getInstance();
DatabaseReference customerRef = database.getReference("Drivers/"+driverId+"/customerRideId");

答案 1 :(得分:0)

试试这段代码..

        DatabaseReference customerRef = FirebaseDatabase.getInstance().getReference();
    Query query1=customerRef.child("Drivers").child(customerRef.getKey()).child("customerRideId");
    Log.d("TAG_SiX", customerRef.toString());
    query1.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // retriveCustomerID(dataSnapshot);
            Log.d("TAG_eight", dataSnapshot.toString());
            Map<String, String> map =
                    (Map<String, String>) dataSnapshot.getValue();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

答案 2 :(得分:0)

要获取customerRideId媒体资源的价值,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference driverIdRef = rootRef.child("Drivers");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String customerRideId = dataSnapshot.child("customerRideId").getValue(String.class);
        Log.d("TAG", customerRideId);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
driverIdRef.addListenerForSingleValueEvent(valueEventListener);

输出结果为:Sqpb6RFNhMOSiXeCrYj83aeeG7r2