Unable to read data from firebase in android

时间:2017-04-06 17:01:04

标签: android firebase firebase-realtime-database

I am working on android app where Firebase is being used for maintaining mobile back end. I have database in this format on Firebase

{
  "users" : {
    "-Kh2wjxeT-w26opA0yqe" : {
      "email" : "XX@gmail.com",
      "firstName" : "XX",
      "interest" : {
        "interests" : [ "Cricket", "Table Tennis", "Football" ]
      },
      "lastName" : "XXX",
      "password" : "abcd@1234"
    }
  }
}

Users.java

@IgnoreExtraProperties
public class User {

    public String firstName;
    public String lastName;
    public String email;
    public String password;

    public Interest interest;

    public User() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public User(String firstName, String lastName, String email, String password, Interest interest) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.interest = interest;
    }
}

Interest.java

public class Interest {

    public List<String> interests;

    public Interest() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public Interest(List<String> interests) {
        this.interests = interests;
    }
}

I have added addValueEventListener on child "users" in this way

mDatabase.child("users").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot noteDataSnapshot : dataSnapshot.getChildren()) {
            User user = dataSnapshot.getValue(User.class);
            Log.d("Scheduled", user.firstName + user.lastName + user.interest.interests.toString());
        }
    }

    @Override
    public void onCancelled(DatabaseError error) {
        // Failed to read value
    }
});

It is fetching correct data what I have on Firebase but it is not mapping data in Users java object so when I am unable to access values. I am not sure what mistake I am doing here.

Log.d("Scheduled", user.firstName + user.lastName + user.interest.interests.toString());

2 个答案:

答案 0 :(得分:1)

要显示数据,您需要更改以下代码:

mDatabase.child("users").child(userId).addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
        String firstName = dataSnapshot.child("firstName").getValue(String.class); 
        Log.d("Scheduled", firstName); 
    } 

    @Override 
    public void onCancelled(DatabaseError error) { 
    // Failed to read value 
    } 
});   

其中userIdpush()方法生成的唯一ID。

答案 1 :(得分:0)

Are you mapping the dataSnapshot to the correct java class?.

Because I see you have named your file as Users.java and when you getValue, you create object for User.

            User user = dataSnapshot.getValue(User.class);

Instead should it be

Users user = dataSnapshot.getValue(Users.class);

Just curious whether you have named it right.

And also use a Map instead of a List for the Users and it will work for sure.

`