我试图使用模型类来获取数据,但是徒劳。已经有很多问题要问,但是我想我是根据他们说的,如果我有什么遗漏,请告诉我。.
我正在尝试获取类似这样的数据。
FirebaseHelper.firebaseDatabase().getReference().child("rooms").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot roomSnap : dataSnapshot.getChildren()) {
Room room = roomSnap.getValue(Room.class);
//no response
Log.d("createdAt", room.createdAt);
// returns all data in roomSnap
Log.d("roomSnap", roomSnap.toString());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
但是它没有从数据库中获取数据。
数据库:
房间模型
public class Room implements Parcelable {
private static final String TAG = "Room";
public Room() {
}
public String objectID = "";
public String createdAt = "";
public String updatedAt = "";
public String ACL = "";
public String roomNumber = "";
public String roomStatus = "";
public String roomGuestFirstName = "";
public String roomGuestLastName = "";
public String guestArrival = "";
public String guestDepature = "";
protected Room(Parcel in) {
objectID = in.readString();
createdAt = in.readString();
updatedAt = in.readString();
ACL = in.readString();
roomNumber = in.readString();
roomStatus = in.readString();
roomGuestFirstName = in.readString();
roomGuestLastName = in.readString();
guestArrival = in.readString();
guestDepature = in.readString();
}
public static final Creator<Room> CREATOR = new Creator<Room>() {
@Override
public Room createFromParcel(Parcel in) {
return new Room(in);
}
@Override
public Room[] newArray(int size) {
return new Room[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(objectID);
dest.writeString(createdAt);
dest.writeString(updatedAt);
dest.writeString(ACL);
dest.writeString(roomNumber);
dest.writeString(roomStatus);
dest.writeString(roomGuestFirstName);
dest.writeString(roomGuestLastName);
dest.writeString(guestArrival);
dest.writeString(guestDepature);
}
}
我正在使用同一个类来创建空间,并且工作正常,只是我在数据检索中遇到了问题。即使没有错误,logcat也没有任何内容,
答案 0 :(得分:0)
答案 1 :(得分:0)
我认为您的代码存在的问题是您没有将检索到的数据设置为列表或回收站视图(用于显示数据的任何内容),并且如果这无济于事,请尝试设置您的addvalue事件监听器在下面的代码中。 希望对您有帮助。
DatabaseReference ref = database.getReference().child("buddy_plans");
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Buddy buddy = dataSnapshot.getValue(Buddy.class);
listBuddy.add(buddy);
buddyAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});