问题描述
所以我在Firebase中有一个数据库.
So I have a Database in Firebase.
此代码在onCreate()中:
This code is in onCreate():
DatabaseReference mDatabaseRefUser =FirebaseDatabase.getInstance().getReference("Users");
此代码在另一个称为onClick的方法中
This code is in another method that gets called 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));
我试图做的是在每次调用该方法时添加一个新值,而不删除先前/现有的值.
What im trying to do is to add a new value without deleting the previous/existing value everytime the method gets called.
就在我每次单击并启动方法时,先前/现有的值都将被删除,而新值将被添加.但是我都不希望被删除.我希望这个新值也能被添加.我已经尝试过使用getRef和push(),但是我的数据仍然被覆盖.我在这里做什么错了?
For the moment everytime I click and start the method, the previous/existing value gets removed and the new one gets added. But I want neither to be deleted. I want this the new value to be added aswell. I have tried with getRef and push() but my data still gets overwritten. What am I doing wrong here?
这是我第一次调用JSON方法:
This is my JSON 1st time I call the method:
"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
This is my JSON when I call it for the 2nd time
"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()
的情况,也没有用.
I have also tried with getRef().push()
, but that didn't work either. I have also tried without .getRef()
and without .push()
, didnt work either.
推荐答案
我对此问题的唯一解决方案是创建一个新数据库,
My only solution to this issue was to make a new Database,
我添加了这个:
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));
并删除了旧的
这篇关于Firebase数据库使用setValue()覆盖以前的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!