如何从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:
- 并非JSON中的每个属性在
User
类中都有一个字段/属性,Firebase会警告该字段/属性. - JSON的某些属性在
User
类中似乎具有字段/属性,但是名称不匹配.
- Not every property from the JSON has a field/property in the
User
class, which Firebase warns about. - 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:
-
@IgnoreExtraProperties
告诉Firebase忽略它在数据库中找到的,在User
类中没有匹配字段的属性.
在getters/setters上的 -
@PropertyName(...)
告诉Firebase从哪个JSON属性读取此值或将其写入.
吸气器/设置器上的 -
@Exclude()
告诉Firebase注意将此值从User
类写入数据库.
User
类上的-
@IgnoreExtraProperties
on theUser
class tells Firebase to ignore properties it finds in the database that don't have a matching field in theUser
class. -
@PropertyName(...)
on the getters/setters tells Firebase what JSON property to read this value from/write it to. -
@Exclude()
on the getters/setters tells Firebase to note write this value from theUser
class to the database.