我尝试从Firebase实时数据库中读取多个不同的对象。如果我只在数据库中添加一种对象,它就可以工作。如果我添加其他对象,Android应用程序不会中断 - 但似乎不再有任何内容。
例如,我有一个活动,它包含一个包含Machine类型对象的列表。此对象保存在Firebase数据库中。
现在我按照这个逻辑阅读对象:
public ArrayList<Machine> readMachines(){
DatabaseReference mDatabaseRef = FirebaseDatabase.getInstance().getReference();
ChildEventListener childEventListener = mDatabaseRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
machines.clear();
if (dataSnapshot.getKey().equals("Machine")) {
for (DataSnapshot snap : dataSnapshot.getChildren()) {
String key = snap.getKey();
Machine m = snap.getValue(Machine.class);
machines.add(m);
}
}
}
//OTHER OVERRIDE METHODS
});
return machines;
}
此刻此工作正常。如果我添加另一个维护对象的对象,则整个系统会中断。
我不明白这个问题,因为我检查dataSnapshot键是否符合&#34; Machine&#34; -key。
以下是接收维护对象的逻辑:
public ArrayList<RepairHistory> readMaintenance(final String machineID){
DatabaseReference mDatabaseRef = FirebaseDatabase.getInstance().getReference();
ChildEventListener childEventListener = mDatabaseRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
histories.clear();
if(dataSnapshot.getKey().equals("Maintenance")){
for(DataSnapshot snap: dataSnapshot.getChildren()){
String key = snap.getKey();
RepairHistory history = snap.getValue(RepairHistory.class);
if(history.getS_machineID().equals(machineID)){
histories.add(history);
}
}
}
}
//OTHER OVERRIDE METHODS
});
return histories;
}
我使用以下代码将数据保存到数据库:
public void saveMachine(Machine machine){
DatabaseReference refDatabase = FirebaseDatabase.getInstance().getReference();
String keyID = refDatabase.child("Machine").push().getKey();
machine.setI_ID(keyID);
refDatabase.child("Machine").child(keyID).setValue(machine);
}
所以我的问题是,为什么在向数据库添加维护时打破读取逻辑?如果我添加维护,我的数据库看起来像这样: Maintanance是机器的钥匙,我知道哪种机器的维护。
{
"Machine" : {
"-KdVxzbMt9nZJUuLQ-Wh" : {
"i_ID" : "-KdVxzbMt9nZJUuLQ-Wh",
"i_uID" : "UxCvQy2ifmYh7gkT2gxVGYjwwGg1",
"s_MachineLocation" : "Maschinen Standort",
"s_Machinentyp" : "Maschinen Typ",
"s_Name" : "Arturs Machine"
}
},
"Maintenance" : {
"-Kdu_CCqdKBH5P_P4Fwd" : {
"d_repairDate" : {
"date" : 26,
"day" : 0,
"hours" : 11,
"minutes" : 29,
"month" : 1,
"seconds" : 53,
"time" : 1488104993199,
"timezoneOffset" : -60,
"year" : 117
},
"s_Description" : "Example\n",
"s_ID" : "-Kdu_CCqdKBH5P_P4Fwd",
"s_author" : "UxCvQy2ifmYh7gkT2gxVGYjwwGg1",
"s_machineID" : "-KdVxzbMt9nZJUuLQ-Wh"
}
}
}
这是RepairHisto的类:
package logical;
导入android.graphics.Bitmap;
import java.io.Serializable; import java.util.Date;
公共类RepairHistory实现Serializable {
private Date d_repairDate = new Date();
private String s_ID;
private String s_Description;
private String s_machineID;
private RepairImage r_image;
private String s_author;
private Bitmap image;
public RepairHistory() {
}
public RepairHistory(Date d_repairDate, String s_ID, String s_Description, String s_machineID, RepairImage r_image, String s_author, Bitmap image) {
this.d_repairDate = d_repairDate;
this.s_ID = s_ID;
this.s_Description = s_Description;
this.s_machineID = s_machineID;
this.r_image = r_image;
this.s_author = s_author;
this.image = image;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getS_ID() {
return s_ID;
}
public void setS_ID(String s_ID) {
this.s_ID = s_ID;
}
public String getS_machineID() {
return s_machineID;
}
public void setS_machineID(String s_machineID) {
this.s_machineID = s_machineID;
}
public Date getD_repairDate() {
return d_repairDate;
}
public void setD_repairDate(Date d_repairDate) {
this.d_repairDate = d_repairDate;
}
public String getS_Description() {
return s_Description;
}
public void setS_Description(String s_Description) {
this.s_Description = s_Description;
}
public RepairImage getR_image() {
return r_image;
}
public void setR_image(RepairImage r_image) {
this.r_image = r_image;
}
public String getS_author() {
return s_author;
}
public void setS_author(String s_author) {
this.s_author = s_author;
}
}