android 从firebase返回错误的布尔值

cqoc49vn  于 2023-04-04  发布在  Android
关注(0)|答案(2)|浏览(146)

我试图从Firebase数据库读取数据并将其Map到他们应用中的**User**类对象。Firebase中的数据包含一个名为“IsOrganization”的字段,该字段具有布尔值。然而,当开发人员检索数据并将其Map到User类对象时,**isOrganization()**方法始终返回**false**,即使值是**true**在Firebase。我正在寻求帮助,以确定和纠正问题的代码或数据库,以准确地反映布尔值从Firebase。

我的实时Direbase数据形成:

{
  "Address": "Moheshkhali",
  "Email": "xyz@gmail.com",
  "FreeSeat": 0,
  "ID": "HOYs7Dz6KHcQKBfJe8A2Uz6qbB52",
  "Image": "xyz",
  "IsOrganization": true,
  "NID": "052394284539",
  "Name": "ARVOVIRUS",
  "Phone": "01852369741",
  "TotalSeat": 0
}

模型类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;
    }
}

读取数据库并在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) {
                
            }
        });

在Logcat中:isOrganization:: false

fykwrbwg

fykwrbwg1#

你在logcat中得到了false

isOrganization:: false

因为您阅读的是字段的默认值,而不是数据库中的值。Firebase总是尝试使用JavaBean naming conventions将节点内的字段与您的类中存在的字段进行Map。
在你的例子中,这样的Map是不可能的,因为你的User类中的getter isOrganization()不能返回一个名为IsOrganization的字段,因为我看到它存在于你的数据库中。
解决方法很简单。你可以在getter前面使用一个注解,如下所示:

@PropertyName("IsOrganization")
public String isOrganization() { return IsOrganization; }

或者,如果你只想读取IsOrganization字段的值,那么实际上不需要将该节点Map到User类型的对象中。你只能像这样读取字段的值:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = db.child("HOYs7Dz6KHcQKBfJe8A2Uz6qbB52");
//                                                       👇
DatabaseReference isOrganizationRef = uidRef.child("IsOrganization");
isOrganizationRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        if (snapshot.exists()) {
            boolean isOrganization = snapshot.getValue(Boolean.class);
            Log.i("isOrganization: ", String.valueOf(isOrganization));
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Log.d("TAG", error.getMessage()); //Never ignore potential errors!
    }
});

logcat中的结果现在将是:

isOrganization:: true
1qczuiv0

1qczuiv02#

我使用@PropertyName注解将JSON字段显式Map到User类中的字段。下面是更新后的User类:

import com.google.firebase.database.PropertyName;

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;
}

// ... Other getters and setters

@PropertyName("IsOrganization")
public boolean isOrganization() {
    return IsOrganization;
}

@PropertyName("IsOrganization")
public void setOrganization(boolean organization) {
    IsOrganization = organization;
}

// ... Other getters and setters
}

将@PropertyName(“IsOrganization”)注解添加到IsOrganization字段的getter和setter方法中,明确告知Firebase将JSON数据中的IsOrganization字段Map到User类中的IsOrganization字段。这应该可以解决isOrganization()方法返回错误值的问题。

相关问题