我无法通过pojo类对象从Firebase检索嵌套的子级 [英] I cannot retrieve nested child from firebase through pojo class object

查看:27
本文介绍了我无法通过pojo类对象从Firebase检索嵌套的子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据库表:

我在安装过程中通过POJO类在Firebase中保存了一些用户信息,即名称,照片,电话号码.稍后在应用程序中,用户可以添加受信任的联系人,我正在添加它,使用POJO类在同一用户表中创建一个嵌套子级.我的问题是如何从Firebase中获取此受信任的联系人值,即contactname1,contactnumber1?

I am saving some user information in Firebase through POJO class during installation i.e. 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 i.e. contactname1, contactnumber1?

这是我的POJO课:

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

}

这是我保存联系人的方式,它成功保存了在用户表中创建带有嵌套元素contactname1的嵌套子级:-----,contactname2 ----

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) {

        }
    });

}

这就是我试图检索联系人吐司的方式,显示用户名n,但不显示来自dbname1的联系人名

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

推荐答案

您可以使用此架构获取受信任的联系人.您必须在响应类和POJO类之间进行匹配,因为您的代码正在等待uphoto,uphone,uname,地址,contacename1和contactnumber1作为viarable,但是您将uphoto,uphone,uname,address作为变量和一个名为TrustedContact1的对象包含contacename1和contactnumber1,在这种情况下,您应该使用:

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

}

这篇关于我无法通过pojo类对象从Firebase检索嵌套的子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆