Reputation: 184
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.
Upvotes: 0
Views: 304
Reputation: 598901
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:
User
class, which Firebase warns about.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 the User
class tells Firebase to ignore properties it finds in the database that don't have a matching field in the User
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 the User
class to the database.Upvotes: 4