I am trying to read data from a Firebase database and map it to a **User**
class object in their app. The data in Firebase contains a field named "IsOrganization" with a boolean value. However, when the developer retrieves the data and maps it to the User class object, the **isOrganization()**
method always returns **false**
, even though the value is **true**
in Firebase. I am seeking help to identify and correct the issue in the code or database to accurately reflect the boolean value from Firebase.
My Realtime Direbase data formation:
{
"Address": "Moheshkhali",
"Email": "[email protected]",
"FreeSeat": 0,
"ID": "HOYs7Dz6KHcQKBfJe8A2Uz6qbB52",
"Image": "xyz",
"IsOrganization": true,
"NID": "052394284539",
"Name": "ARVOVIRUS",
"Phone": "01852369741",
"TotalSeat": 0
}
Model class User.java:
public class User {
private String ID, Name, Email, Phone, NID, Address, Image;
private boolean IsOrganization;
private int TotalSeat, FreeSeat;
public User() {}
public User(String ID, String name, String email, String phone, String NID, String address,
String image, boolean isOrganization, int totalSeat, int freeSeat) {
this.ID = ID;
this.Name = name;
this.Email = email;
this.Phone = phone;
this.NID = NID;
this.Address = address;
this.Image = image;
this.IsOrganization = isOrganization;
this.TotalSeat = totalSeat;
this.FreeSeat = freeSeat;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getNID() {
return NID;
}
public void setNID(String NID) {
this.NID = NID;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public boolean isOrganization() {
return IsOrganization;
}
public void setOrganization(boolean organization) {
IsOrganization = organization;
}
public int getTotalSeat() {
return TotalSeat;
}
public void setTotalSeat(int totalSeat) {
TotalSeat = totalSeat;
}
public int getFreeSeat() {
return FreeSeat;
}
public void setFreeSeat(int freeSeat) {
FreeSeat = freeSeat;
}
}
For read database and show in the app:
databaseReference.child(HOYs7Dz6KHcQKBfJe8A2Uz6qbB52)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
User users = snapshot.getValue(User.class);
boolean isOrganization = users.isOrganization();
Log.i("isOrganization: ", String.valueOf(isOrganization )); // It show flase in logcat
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
In Logcat:
isOrganization:: false
2
Answers
I use the @PropertyName annotation to explicitly map the JSON field to the field in the User class. Here's the updated User class:
Adding the @PropertyName("IsOrganization") annotation to both the getter and setter methods for the IsOrganization field explicitly tells Firebase to map the IsOrganization field in JSON data to the IsOrganization field in your User class. This should fix the issue with the isOrganization() method returning an incorrect value.
You’re getting
false
in the logcat:Because you’re reading the default value of the field and not the value from the database. Firebase always tries to map the fields inside a node with the fields that exist in your class using JavaBean naming conventions.
In your case, such a mapping isn’t possible because the getter
isOrganization()
in yourUser
class cannot return a field calledIsOrganization
as I see it exists in your database. See the capital letterI
?The solutions are quite simple. You can use an annotation in front of the getter like this:
Or, if you only want to read the value of the
IsOrganization
field, then there is actually no need to map that node into an object of typeUser
. You can only read the value of the field like this:The result in the logcat will be now: