如何从Firebase通过用户模型获取用户数据? [英] How to get User data Via User Model from firebase?

查看:81
本文介绍了如何从Firebase通过用户模型获取用户数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常努力;但是,我无法通过应用程序个人资料片段中的用户模型获取用户数据.这是我在项目中编写的代码.

I tried very hard; but, I can't able to get user data via user Model in Profile Fragment in my app. Here is the code which I have written in my project.

ProfileFragment.java

ProfileFragment.java

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        User user = dataSnapshot.getValue(User.class);
        assert user != null;
        username.setText(user.getUserName());
        useremail.setText(user.getUserEmail());
        userphone.setText(user.getUserPhone());
        userdesc.setText(user.getUserDesc());
        if(user.getImageURL().equals("default")){
            profile_image.setImageResource(R.drawable.round_logo);
        }else {
            Picasso.with(getContext())
                    .load(user.getImageURL())
                    .placeholder(R.drawable.round_logo)
                    .into(profile_image);
        }
    }

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

    }
});

User.java

User.java

package com.psb.farmersmarket.Model;

public class User {
    private String UserName;
    private String UserPhone;
    private String UserDesc;
    private String UserEmail;
    private String UserID;
    private String imageURL;

    public User(String userName,String userPhone,String userDesc,String userEmail, String userID, String imageURL) {
        this.UserName = userName;
        this.UserPhone = userPhone;
        this.UserDesc = userDesc;
        this.UserEmail = userEmail;
        this.UserID = userID;
        this.imageURL = imageURL;
    }

    public User() {
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getUserPhone() {
        return UserPhone;
    }

    public void setUserPhone(String userPhone) {
        UserPhone = userPhone;
    }

    public String getUserDesc() {
        return UserDesc;
    }

    public void setUserDesc(String userDesc) {
        UserDesc = userDesc;
    }

    public String getUserEmail() {
        return UserEmail;
    }

    public void setUserEmail(String userEmail) {
        UserEmail = userEmail;
    }

    public String getUserID() {
        return UserID;
    }

    public void setUserID(String userID) {
        UserID = userID;
    }

    public String getImageURL() {
        return imageURL;
    }

    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }
}

请参见上方代码.我写了所有的getter&用户模型中的setter方法.即使错误表明用户没有设置方法.该错误也在下面给出.

See the upper code. I wrote all getter & setter methods in the user model. Even though the error says the user has no setter methods. The error is also given below.

错误消息

Error Message

W/ClassMapper: No setter/field for search found on class com.psb.farmersmarket.Model.User
W/ClassMapper: No setter/field for Description found on class com.psb.farmersmarket.Model.User
    No setter/field for name found on class com.psb.farmersmarket.Model.User
    No setter/field for user id found on class com.psb.farmersmarket.Model.User
    No setter/field for email found on class com.psb.farmersmarket.Model.User
W/ClassMapper: No setter/field for Phone No found on class com.psb.farmersmarket.Model.User
    No setter/field for status found on class com.psb.farmersmarket.Model.User
W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.

数据库

Database

请帮助我.在此先感谢.

推荐答案

从错误消息中,您的JSON看起来像这样:

From the error message, it seems like your JSON looks like this:

"userid": {
  "search": "value", 
  "name": "value",
  "user id": "value",
  "email": "value",
  "Phone No": "value",
  "status": "value",
  "airplane_mode": "value"
}

此JSON与您的代码之间存在两个问题:

There are two problems between this JSON and your code:

  1. 并非JSON中的每个属性在User类中都有一个字段/属性,Firebase会警告该字段/属性.
  2. JSON的某些属性在User类中似乎具有字段/属性,但是名称不匹配.
  1. Not every property from the JSON has a field/property in the User class, which Firebase warns about.
  2. Some properties from the JSON seem to have a field/property in the User class, but the name doesn't match.

您可以通过使用Java代码中的注释来修复这两个问题:

You can fix both of these by using annotations in the Java code:

@IgnoreExtraProperties
public class User {
    private String UserName;
    private String UserPhone;
    private String UserDesc;
    private String UserEmail;
    private String UserID;
    private String imageURL;

    public User(String userName,String userPhone,String userDesc,String userEmail, String userID, String imageURL) {
        this.UserName = userName;
        this.UserPhone = userPhone;
        this.UserDesc = userDesc;
        this.UserEmail = userEmail;
        this.UserID = userID;
        this.imageURL = imageURL;
    }

    public User() {
    }

    @PropertyName("name")
    public String getUserName() {
        return UserName;
    }

    @PropertyName("name")
    public void setUserName(String userName) {
        UserName = userName;
    }

    @PropertyName("Phone No")
    public String getUserPhone() {
        return UserPhone;
    }

    @PropertyName("Phone No")
    public void setUserPhone(String userPhone) {
        UserPhone = userPhone;
    }

    @Exclude
    public String getUserDesc() {
        return UserDesc;
    }

    @Exclude
    public void setUserDesc(String userDesc) {
        UserDesc = userDesc;
    }

    @PropertyName("email")
    public String getUserEmail() {
        return UserEmail;
    }

    @PropertyName("email")
    public void setUserEmail(String userEmail) {
        UserEmail = userEmail;
    }

    @PropertyName("user id")
    public String getUserID() {
        return UserID;
    }

    @PropertyName("user id")
    public void setUserID(String userID) {
        UserID = userID;
    }

    @Exclude
    public String getImageURL() {
        return imageURL;
    }

    @Exclude
    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }
}

您将在此处看到三个注释:

You'll see we have three annotations in here:

    User类上的
  1. @IgnoreExtraProperties告诉Firebase忽略它在数据库中找到的,在User类中没有匹配字段的属性.
  2. 在getters/setters上的
  3. @PropertyName(...)告诉Firebase从哪个JSON属性读取此值或将其写入.
  4. 吸气器/设置器上的
  5. @Exclude()告诉Firebase注意将此值从User类写入数据库.
  1. @IgnoreExtraProperties on the User class tells Firebase to ignore properties it finds in the database that don't have a matching field in the User class.
  2. @PropertyName(...) on the getters/setters tells Firebase what JSON property to read this value from/write it to.
  3. @Exclude() on the getters/setters tells Firebase to note write this value from the User class to the database.

这篇关于如何从Firebase通过用户模型获取用户数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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