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":"abc@gmail.com",
   2:
    "userEmail":"xyz@gmail.com"     
 },
"second":{
   1:
    "userEmail":"abc@gmail.com",
   2:
    "userEmail":"xyz@gmail.com"     
}
}

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 = "abc@gmail.com";
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":"abc@gmail.com"
 }
}

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
  • Can you post the code of class ViewedUsers? – Nick Zisis Aug 22 '17 at 12:13
  • @NickZisis I have updated the Model Class ViewedUsers. – Ajay Jayendran Aug 22 '17 at 12:15
  • Firebase is always saving key-value pairs. Look at [this link](https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html) how to handle arrays with firebase. – Simon B. Aug 22 '17 at 12:18
  • @SimonB. okay.now if I send the userEmail to firebase it overwrites. – Ajay Jayendran Aug 22 '17 at 12:20
  • if you use "userEmail" as your key, it will be overriden, because you have the same key. You may try to use the email as key as @AndiM suggests in his answer. Also have a look at the linked page of my last comment. – Simon B. Aug 22 '17 at 12:27
  • @SimonB. can you look up the format now.? how to add value like this in android – Ajay Jayendran Aug 22 '17 at 13:06
  • Probably you may try something like this: DatabaseReference emailRef = mailFireBaseDb.getReference("userviews/first"); Map email = new HashMap<>(); email.put("xyz@mail.com", 0); emailRef.updateChildren(email) – Simon B. Aug 22 '17 at 13:10

1 Answers1

0

UPDATE

"userviews" :{
 "first":{
   0:"abc@gmail.com",
   1:"xyz@gmail.com"     
 },
"second":{
   0:"abc@gmail.com",
   1:"xyz@gmail.com"     
}
}

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":{
   "abc@gmail.com":0
   "xyz@gmail.com":0   
 },
"second":{
   "abc@gmail.com":0
   "xyz@gmail.com":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
  • thanks :)..okay now I understood.Now I have updated the format..can you look up the format.?Suppose if I add userEmail to "first" child , it looks like this , "userviews" :{ "first":{ 1: "userEmail":"abc@gmail.com", 2: "userEmail":"xyz@gmail.com" , 3: "userEmail":"cde@gmail.com" }, "second":{ 1: "userEmail":"abc@gmail.com", 2: "userEmail":"xyz@gmail.com" } } – Ajay Jayendran Aug 22 '17 at 13:05
  • No you are still making mistakes in understanding firebase array. If you want to use array then you don't need to have "userEmail" key for each email.It will be totally omitted. See my updated answer for your new firebase structure. – AndiM Aug 23 '17 at 04:41