简体   繁体   中英

I cannot retrieve nested child from firebase through pojo class object

This is my database table:

数据库表

I am saving some user information in Firebase through POJO class during installation ie name, photo, phone number. Later on in app user can add trusted contact I am adding it creating a nested child in same user table using POJO class. My question is how to get this trustedcontact value from firebase ie contactname1, contactnumber1?

This is my POJO class:

package com.example.fizatanveerkhan.citycops;

import java.util.HashMap;

public class UserInformation {

    private String uphone;
    private String uphoto ;
    private String uname;
    private String address;

    private String contactname1;
    private String contactnumber1;

    public UserInformation()
    {

    }

    public UserInformation(String phonenumber, String uphoto,String uname, String add, String cn1, String cb1)
    {
        this.uphone=phonenumber;
        this.uphoto=uphoto;
        this.uname= uname;
        this.address=add;
        this.contactname1=cn1;
        this.contactnumber1=cb1;
    }


    public void setUphone(String uphone) {
        this.uphone = uphone;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public void setUphoto(String uphoto) {
        this.uphoto = uphoto;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setContactname1(String contactname1) {
        this.contactname1 = contactname1;
    }

    public String getUphoto() {
        return uphoto;
    }

    public String getUphone() {
        return uphone;
    }

    public String getUname() {
        return uname;
    }

    public String getAddress(){return address;}

    public String getContactname1() {
        return contactname1;
    }


    public String getContactnumber1() {
        return contactnumber1;
    }


    public void setContactnumber1(String contactnumber1) {
        this.contactnumber1 = contactnumber1;
    }

}

This is how I am saving contact it is saving successfully creating a nested child in users table with nested elements contactname1: -----, contactname2----

public void upload()
    {

    firebaseDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


            UserInformation upload1 = new UserInformation();

        if(number1 != null )
        {  upload1.setContactnumber1(number1);
           if(name1 != null)
           {
               upload1.setContactname1(name1);
           }
          firebaseDatabase.child("TrustedContact1").setValue(upload1);
        }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

And this is how I am trying to retrieve contact toast shows the user name n but not contactname fromdbname1

firebaseDatabase= database.getReference("USERS").child(uid);


ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        if (dataSnapshot.exists()) {

            current_user = dataSnapshot.getValue(UserInformation.class);
            String n = current_user.getUname();

            fromdbname1 = current_user.getContactname1();
            Toast.makeText(getApplicationContext(), fromdbname1, 
            Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(), n, 
            Toast.LENGTH_LONG).show();

        }
    }
    @Override
    public void onCancelled (DatabaseError databaseError){
        Toast.makeText(getApplicationContext(), "Error Loading UserDetails", 
        Toast.LENGTH_LONG).show();
    }
};
firebaseDatabase.addListenerForSingleValueEvent(eventListener);

You can get the trusted contact using this schema . you have to match between response class and POJO class ,because you code is waiting for uphoto,uphone,uname,address,contacename1 and contactnumber1 as viarables,but you have uphoto,uphone,uname,address as variables and an object that named TrustedContact1 that contains contacename1 and contactnumber1 , in you case you should use :

package com.example.fizatanveerkhan.citycops;

import java.util.HashMap;

public class UserInformation {

    private String uphone;
    private String uphoto ;
    private String uname;
    private String address;

    private TrustedContact1 TrustedContact1;

    // use inner class on create class in another file
    class TrustedContact1{
       public TrustedContact1(){
       }
       private String contactname1;
       private String contactnumber1;
   }

    // getters and seeters

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM