所以我在Firebase中有一个数据库。
此代码位于onCreate():
DatabaseReference mDatabaseRefUser =FirebaseDatabase.getInstance().getReference("Users");
此代码在另一个调用onClick:
的方法中mDatabaseRefUser.child("Chat").child(mAuth.getCurrentUser().getUid())
.child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
mAuth.getCurrentUser().getEmail()));
mDatabaseRefUser.child("Chat").child(userID)
.child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
userEmail));
我想要做的是在每次调用方法时添加新值而不删除先前/现有值。
目前,每次单击并启动方法时,都会删除先前/现有值,并添加新值。但我不希望被删除。我希望这个新值也可以添加。我尝试过getRef和push(),但我的数据仍然被覆盖。我在这里做错了什么?
这是我第一次调用方法时的JSON:
"Users" : {
"Chat" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
"xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"-LCvIA_9L1Y9rjkm96Aj" : {
"Messages" : {
"-LCvqDidnNqQ8ALZhDud" : {
"messageText" : "Very good",
"messageTime" : 1526791230552,
"messageUser" : "mama@mama.com"
}
},
"chatAgainstUserEmail" : "mama2@mama.com",
"chatAgainstUserID" : "xfqAFlRpbUZRlZb76svN5FUtbU93",
"uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
"userEmail" : "mama@mama.com",
"userID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2"
}
}
},
"xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
"-LCvIA_9L1Y9rjkm96Aj" : {
"Messages" : {
"-LCvqDigI8ZwHDYuPhw9" : {
"messageText" : "Very good",
"messageTime" : 1526791230555,
"messageUser" : "mama2@mama.com"
}
},
"chatAgainstUserEmail" : "mama@mama.com",
"chatAgainstUserID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2",
"uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
"userEmail" : "mama2@mama.com",
"userID" : "xfqAFlRpbUZRlZb76svN5FUtbU93"
}
}
}
}
这是我第二次打电话时的JSON
"Users" : {
"Chat" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
"xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"-LCvIA_9L1Y9rjkm96Aj" : {
"Messages" : {
"-LCvr0zoHOPxsKOVJzeu" : {
"messageText" : "Very good 2nd call",
"messageTime" : 1526791440547,
"messageUser" : "mama@mama.com"
}
},
"chatAgainstUserEmail" : "mama2@mama.com",
"chatAgainstUserID" : "xfqAFlRpbUZRlZb76svN5FUtbU93",
"uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
"userEmail" : "mama@mama.com",
"userID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2"
}
}
},
"xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
"-LCvIA_9L1Y9rjkm96Aj" : {
"Messages" : {
"-LCvr0zrh8wQ2WurUezs" : {
"messageText" : "Very good 2nd call",
"messageTime" : 1526791440550,
"messageUser" : "mama2@mama.com"
}
},
"chatAgainstUserEmail" : "mama@mama.com",
"chatAgainstUserID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2",
"uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
"userEmail" : "mama2@mama.com",
"userID" : "xfqAFlRpbUZRlZb76svN5FUtbU93"
}
}
}
}
我也试过了getRef().push()
,但这也没有用。我也试过没有。getRef()
而没有.push()
,也没用。
答案 0 :(得分:0)
要更新数据,您应该使用此类代码,而不是使用child.setValue()
,因为它的名称表明它会直接用新值替换旧值。但是如果你想改变一个特定的价值,你应该采取如下的措施。试一试吧!
HashMap<String, Object> params = new HashMap<>();
params.put("yourKey", your value);
yourDatabaseChildReference.updateChildren(params);
答案 1 :(得分:-1)
我对此问题的唯一解决方案是创建一个新的数据库,
我补充说:
mDatabaseRefUser.child("Chatting").child(mAuth.getCurrentUser().getUid())
.child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
mAuth.getCurrentUser().getEmail()));
mDatabaseRefUser.child("Chatting").child(userID)
.child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
userEmail));
并删除旧的