英文:
How to get User data Via User Model from firebase?
问题
我非常努力地尝试了,但是在我的应用程序的**个人资料片段(Profile Fragment)中,无法通过用户模型(User Model)**获取用户数据。以下是我在项目中编写的代码。
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
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方法。尽管如此,错误仍然说用户没有setter方法。错误消息如下:
错误消息
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.
数据库
请帮帮我。提前谢谢。
英文:
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
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
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;
}
}
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
Please help me. Thanks in Advance.
答案1
得分: 4
错误信息显示,你的 JSON 数据如下:
"userid": {
"search": "value",
"name": "value",
"user id": "value",
"email": "value",
"Phone No": "value",
"status": "value",
"airplane_mode": "value"
}
在这个 JSON 数据和你的代码之间存在两个问题:
- 并非 JSON 中的每个属性都在
User
类中有相应的字段/属性,这是 Firebase 给出的警告。 - JSON 中的一些属性似乎在
User
类中有相应的字段/属性,但名称不匹配。
你可以通过在 Java 代码中使用注解来解决这两个问题:
@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;
}
}
你可以看到我们在这个代码中使用了三个注解:
@IgnoreExtraProperties
注解在User
类上,告诉 Firebase 忽略数据库中存在但在User
类中没有对应字段的属性。- 在 getters 和 setters 方法上使用
@PropertyName(...)
注解,告诉 Firebase 从哪个 JSON 属性读取该值或将其写入。 - 在 getters 和 setters 方法上使用
@Exclude()
注解,告诉 Firebase 不将该值从User
类写入数据库。
英文:
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"
}
There are two problems between this JSON and your code:
- 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.
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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论