0

I need to store the list of values in following the database structure.Also how to get the list of particular post's("first") values.

"userviews" :{
 "first":{
   1:
   "userEmail":"[email protected]",
   2:
    "userEmail":"[email protected]"     
 },
"second":{
   1:
    "userEmail":"[email protected]",
   2:
    "userEmail":"[email protected]"     
}
}

I am developing similar to blog application.Here "userviews" is the root of the database." first" and "second" are post titles.Suppose if the user has seen the post "first", their mail has been sent to "first" list in the Firebase database.

I tried with this following solution but I didn't get proper format

String userEmail = "[email protected]";
ViewedUsers viewedUsers = new ViewedUsers();
viewedUsers.setUserEmail(userEmail);
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("userviews").child("first").setValue(viewedUsers);

if I doing like this, I am getting following format.

"userviews" :{
 "first":{
 "userEmail":"[email protected]"
 }
}

Model Class : ViewedUser

    public class ViewedUsers {

        public String getUserEmail() {
            return userEmail;
        }

        public void setUserEmail(String userEmail) {
            this.userEmail = userEmail;
        }
    String userEmail;
}
Ajay Jayendran
  • 1,584
  • 4
  • 15
  • 37

1 Answers1

0

UPDATE

"userviews" :{
 "first":{
   0:"[email protected]",
   1:"[email protected]"     
 },
"second":{
   0:"[email protected]",
   1:"[email protected]"     
}
}

OLD

As per my knowledge and experience, Firebase is totally depends on key value parameters if I am not wrong. Please let me know if I am wrong somewhere.

If you don't want to have keys in your database then what can you do is convert your values in key. Something like this:

"userviews" :{
 "first":{
   "[email protected]":0
   "[email protected]":0   
 },
"second":{
   "[email protected]":0
   "[email protected]":0
}
}

This way your values become your keys and you can easily retrieve it as you retrieve your values.

You can also save it as array. As suggested by @Simon B

For fetching particular node you may refer this link and this. It is just simple. If you still found any issue please let me know.

AndiM
  • 2,196
  • 2
  • 21
  • 38