我正在尝试从Firebase中的一个节点获取所有值并将其转换为我的对象(我不想创建这些值的映射并将每个值设置为我的对象)我正在使用此代码下面
dbRef = FirebaseDatabase.getInstance().getReference();
contactRef =
dbRef.child(Constants.USERS).child(userId).child(Constants.CONTACTS);
contactRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Contact contact = ds.getValue(Contact.class);
}
}
但这给了我错误
Can't convert object of type java.lang.Boolean to type ...Objects.Contact
如果我只是从datasnapshot中打印值,则会得到预期的输出(为简洁起见,已删除了这些值)
Log.d(TAG,ds.toString());
key = blocked, value =
key = msg_time_stamp, value =
key = registeredToken, value =
key = unread, value =
key = user_id, value =
key = user_image, value =
key = user_name, value =
key = user_number, value =
key = user_recent_message, value =
key = user_small_image, value =
key = user_status, value =
key = user_time_stamp, value =
与我的班级相符的
private String registeredToken;
private String user_id;
private String user_name;
private String user_status;
private String user_number;
private String user_image;
private String user_small_image;
private long user_time_stamp;
private String user_recent_message;
private long msg_time_stamp;
private Boolean blocked;
private int unread;
有人知道为什么这可能会失败吗?这是我完整的Contacts对象类
public class Contact implements Parcelable {
public static final Creator CREATOR = new Creator() {
public Contact createFromParcel(Parcel parcel) {
return new Contact(parcel);
}
public Contact[] newArray(int size) {
return new Contact[size];
}
};
private String registeredToken;
private String user_id;
private String user_name;
private String user_status;
private String user_number;
private String user_image;
private String user_small_image;
private long user_time_stamp;
private String user_recent_message;
private long msg_time_stamp;
private Boolean blocked;
private int unread;
public Contact() {
}
public String getUser_name() {
return this.user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_status() {
return this.user_status;
}
public void setUser_status(String user_status) {
this.user_status = user_status;
}
public String getUser_id() {
return this.user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_number() {
return this.user_number;
}
public void setUser_number(String user_number) {
this.user_number = user_number;
}
public String getUser_image() {
return this.user_image;
}
public void setUser_image(String user_image) {
this.user_image = user_image;
}
public String getUser_small_image() {
return user_small_image;
}
public void setUser_small_image(String user_small_image) {
this.user_small_image = user_small_image;
}
public long getUser_time_stamp() {
return this.user_time_stamp;
}
public void setUser_time_stamp(long user_time_stamp) {
this.user_time_stamp = user_time_stamp;
}
public String getUser_recent_message() {
return this.user_recent_message;
}
public void setUser_recent_message(String user_recent_message) {
this.user_recent_message = user_recent_message;
}
public Boolean getBlocked() {
return this.blocked;
}
public void setBlocked(Boolean blocked) {
this.blocked = blocked;
}
public int getUnread() {
return this.unread;
}
public void setUnread(int unread) {
this.unread = unread;
}
public long getMsg_time_stamp() {
return this.msg_time_stamp;
}
public void setMsg_time_stamp(long msg_time_stamp) {
this.msg_time_stamp = msg_time_stamp;
}
public String getRegisteredToken() {
return registeredToken;
}
public void setRegisteredToken(String registeredToken) {
this.registeredToken = registeredToken;
}
@Override
public boolean equals(Object otherUserObject) {
if (!(otherUserObject instanceof Contact)) {
return false;
}
Contact that = (Contact) otherUserObject;
if (this.user_recent_message != null && that.user_recent_message != null){
return this.user_name.equals(that.user_name)
&& this.user_status.equals(that.user_status)
&& this.user_id.equals(that.user_id)
&& this.user_number.equals(that.user_number)
&& this.user_image.equals(that.user_image)
&& this.user_small_image.equals(that.user_small_image)
&& this.user_recent_message.equals(that.user_recent_message)
&& this.unread == that.unread
&& this.blocked == that.blocked
&& this.user_time_stamp == that.user_time_stamp
&& this.msg_time_stamp == that.msg_time_stamp
&& this.registeredToken.equals(that.registeredToken)
;
}
else{
return this.user_name.equals(that.user_name)
&& this.user_status.equals(that.user_status)
&& this.user_id.equals(that.user_id)
&& this.user_number.equals(that.user_number)
&& this.user_image.equals(that.user_image)
&& this.user_small_image.equals(that.user_small_image)
&& this.blocked == that.blocked
&& this.unread == that.unread
&& this.user_time_stamp == that.user_time_stamp
&& this.registeredToken.equals(that.registeredToken)
;
}
}
private Contact(Parcel in){
this.user_name = in.readString();
this.user_status = in.readString();
this.user_id = in.readString();
this.user_number = in.readString();
this.user_image = in.readString();
this.user_small_image = in.readString();
this.user_time_stamp = in.readLong();
this.user_recent_message = in.readString();
this.msg_time_stamp = in.readLong();
this.blocked = in.readInt() != 0;
this.unread = in.readInt();
this.registeredToken = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.user_name);
dest.writeString(this.user_status);
dest.writeString(this.user_id);
dest.writeString(this.user_number);
dest.writeString(this.user_image);
dest.writeString(this.user_small_image);
dest.writeLong(this.user_time_stamp);
dest.writeString(this.user_recent_message);
dest.writeLong(this.msg_time_stamp);
dest.writeInt(this.blocked ? 1 : 0);
dest.writeInt(this.unread);
dest.writeString(this.registeredToken);
}
public static class ContactsList {
private List<Contact> contactsList;
public List<Contact> getContactList() {
return contactsList;
}
}
}
答案 0 :(得分:0)
将子事件监听器替换为标准事件监听器显然是我的问题