【问题标题】:Reading child in Firebase Database在 Firebase 数据库中读取孩子
【发布时间】:2020-03-30 08:17:10
【问题描述】:

我想根据子节点的特定键访问用户中的子项,并且我已经在我的函数 getcount() 中传递了该键,但是当我使用该引用键访问用户时,它显示 com.google.firebase .database.DatabaseException: 无法将 java.lang.Long 类型的值转换为 String。

我使用了以下代码,但它不能正常工作

  private void getCount(final String referalKey) {
    DatabaseReference myref=FirebaseDatabase.getInstance().getReference().child("users").child(referalKey);
    myref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            user u=dataSnapshot.getValue(user.class);
            Toast.makeText(Home.this,u.getCount(),Toast.LENGTH_SHORT).show();

        }

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

        }
    });



} 

我的用户模型类

public class user {
      public String name;
      public String phone;
      public String refphone;
      public String address;
      public String count;

    public user() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    public user(String name, String phone, String refphone, String address, String count) {
        this.name = name;
        this.phone = phone;
        this.refphone = refphone;
        this.address = address;
        this.count = count;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setRefphone(String refphone) {
        this.refphone = refphone;
    }

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

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public String getRefphone() {
        return refphone;
    }

    public String getAddress() {
        return address;
    }
}

【问题讨论】:

  • 添加您的user 模型
  • 我已经添加了我的用户模型类
  • 在哪里?没看到
  • 我的意思是User.java,不是数据库结构
  • 现在检查我已经更新了我的问题

标签: android database firebase firebase-realtime-database firebase-authentication


【解决方案1】:

正如我在您的数据库中看到的那样,在其中一个文档中截屏您的计数和电话号码,它位于 " 中,而在其他文档中则很长。手动修改

【讨论】:

  • 是的,这是我在输入导致数据类型不正确匹配的数据时错过了双引号的主要事情 感谢您的回答
【解决方案2】:

将您的phonecount 属性从String 更改为long,因为您的数据库将其包含为long

public class user {
    public String name;
    public long phone;
    public String refphone;
    public String address;
    public long count;

    public user() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public user(String name, long phone, String refphone, String address, long count) {
        this.name = name;
        this.phone = phone;
        this.refphone = refphone;
        this.address = address;
        this.count = count;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(long phone) {
        this.phone = phone;
    }

    public void setRefphone(String refphone) {
        this.refphone = refphone;
    }

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

    public String getName() {
        return name;
    }

    public long getPhone() {
        return phone;
    }

    public String getRefphone() {
        return refphone;
    }

    public String getAddress() {
        return address;
    }
}

【讨论】:

  • 即使他使用你的用户模型也行不通。导致插入数据库的问题。他需要从数据库中手动更改或删除字符串中的节点
  • 是的,这是在数据库中插入数据的问题,感谢您的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-07
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 2020-12-17
相关资源
最近更新 更多