0

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());

N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • In your `Interest` class, in stead of using a `List` use a `Map`. It will work for sure. And don't forget to set, public setters and getter in your model class. – Alex Mamo Apr 06 '17 at 17:06
  • I guess problem because of created unique Id's ` "-Kh2wjxeT-w26opA0yqe" and I have nothing like that in `User.java` – N Sharma Apr 06 '17 at 17:15
  • @AlexM. I tried `HashMap also but it is not working – N Sharma Apr 06 '17 at 17:24
  • Williams, please remodel your class. Remove your `Interest` class and in `User` class create a `Map` named `interest`. And than create public setters and getters. – Alex Mamo Apr 06 '17 at 17:47
  • @AlexM. I have done https://pastebin.com/hvM8TxbK it is not working – N Sharma Apr 06 '17 at 17:59
  • No, you haven't done. I repet, you need to create a `Map` in your `User` class. First declare a field in `User` class: `Map interests`, than create public setter and getters, in the same way you did with the others (firstName, lastName etc). When you'll create the object of `User` class, you can use the constructor or the setter method to set the `Map`. And when you'll try to `Log`, you need to use something like this: `user.getInterest` which will be a map, and than you will be able to iterate over it. Is it now clear? – Alex Mamo Apr 06 '17 at 18:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/140104/discussion-between-williams-and-alex-m). – N Sharma Apr 06 '17 at 18:15

2 Answers2

1

In order to display the data, you need change your code like this:

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 
    } 
});   

In which userId is the unique id generated by the push() method.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
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.

`