Android 如何在Firebase中保存数据而不覆盖当前数据?
我希望允许用户在不重写UID下存储的信息的情况下发布大量帖子。我知道push方法会随机生成密钥,但我不希望使用它。我们将非常感谢您的帮助。在下面的方法中,我基本上将包含用户信息的字典保存到实时数据库中。但由于采用了设定值方法,信息正在被替换 数据库:Android 如何在Firebase中保存数据而不覆盖当前数据?,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我希望允许用户在不重写UID下存储的信息的情况下发布大量帖子。我知道push方法会随机生成密钥,但我不希望使用它。我们将非常感谢您的帮助。在下面的方法中,我基本上将包含用户信息的字典保存到实时数据库中。但由于采用了设定值方法,信息正在被替换 数据库: Post NEbIdbCumcOnIbPdUQxpwtV7Dr63 (UID) desc: "Toue moe" id: "NEbIdbCumcOnIbPdUQxpwtV7Dr63" image: "Contains some imag
Post
NEbIdbCumcOnIbPdUQxpwtV7Dr63 (UID)
desc: "Toue moe"
id: "NEbIdbCumcOnIbPdUQxpwtV7Dr63"
image: "Contains some image"
name: "Some name"
profileimage: "Image"
代码:
public void saveToFirebase(){
final ProgressDialog ProgressDialog=新建ProgressDialog(此);
progressDialog.setTitle(“上载…”);
progressDialog.show();
//最后一个字符串userId=mAuth.getCurrentUser().getUid();
final StorageReference storageRef=FirebaseStorage.getInstance().getReference().child(“图像”).child(用户ID);
storageRef.putFile(selectedImageUri).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
storageRef.getDownloadUrl().addOnSuccessListener(新OnSuccessListener()){
@凌驾
成功时的公共无效(Uri){
url=uri.toString();
postdirectionary.put(“desc”,descriptionEditText.getText().toString());
postDictionary.put(“图像”,url);
postdirectionary.put(“id”,userId);
postDictionary.put(“name”,name);
postdirectionary.put(“profileimage”,profileimage);
productsDatabaseRef.child(“Post”).child(userId).setValue(postDictionary);
progressDialog.disclose();
Intent Intent=新的Intent(上传Post.this、Shop\u Activity.class);
星触觉(意向);
完成();
}
//处理错误
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
progressDialog.disclose();
Toast.makeText(Upload_Post.this,“Error”+e.getMessage(),Toast.LENGTH_SHORT.show();
}
});
}
}
});
}
我也有同样的问题。但我通过添加时间戳/日期解决了这个问题。
只是个主意!
这里
我的意思是在uid中创建一个子对象作为(更新帖子的当前时间)。
然后在当前时间内插入新帖子
要实现该功能,请将其添加到java文件中-
import java.text.SimpleDateFormat;
import java.util.Date;
//定义日期和时间并将其存储在currenttime变量中
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
String currenttime = sdf.format(new Date());
现在在这里做如下更改
final StorageReference storageRef = FirebaseStorage.getInstance().getReference().child("images").child(userId);
storageRef.putFile(selectedImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
String currenttime = sdf.format(new Date());
storageRef.child(currenttime).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
{
@Override
public void onSuccess(Uri uri) {
url = uri.toString();
postDictionary.put("desc", descriptionEditText.getText().toString());
postDictionary.put("image", url);
postDictionary.put("id", userId);
postDictionary.put("name",name);
postDictionary.put("profileimage", profileImage);
productsDatabaseRef.child("Post").child(userId).setValue(postDictionary);
progressDialog.dismiss();
Intent intent = new Intent(Upload_Post.this, Shop_Activity.class);
startActivity(intent);
finish();
}
我也有同样的问题。但我通过添加时间戳/日期解决了这个问题。 只是个主意! 这里 我的意思是在uid中创建一个子对象作为(更新帖子的当前时间)。 然后在当前时间内插入新帖子 要实现该功能,请将其添加到java文件中-
import java.text.SimpleDateFormat;
import java.util.Date;
//定义日期和时间并将其存储在currenttime变量中
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
String currenttime = sdf.format(new Date());
现在在这里做如下更改
final StorageReference storageRef = FirebaseStorage.getInstance().getReference().child("images").child(userId);
storageRef.putFile(selectedImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
String currenttime = sdf.format(new Date());
storageRef.child(currenttime).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
{
@Override
public void onSuccess(Uri uri) {
url = uri.toString();
postDictionary.put("desc", descriptionEditText.getText().toString());
postDictionary.put("image", url);
postDictionary.put("id", userId);
postDictionary.put("name",name);
postDictionary.put("profileimage", profileImage);
productsDatabaseRef.child("Post").child(userId).setValue(postDictionary);
progressDialog.dismiss();
Intent intent = new Intent(Upload_Post.this, Shop_Activity.class);
startActivity(intent);
finish();
}
如果要允许每个用户上载多个图像,请调用
push()
为每个图像生成新的子节点:
productsDatabaseRef.child("Post").child(userId).push().setValue(postDictionary);
调用
push()。要了解有关这些ID的更多信息,请参阅。如果要允许每个用户上载多个图像,请调用push()
为每个图像生成新的子节点:
productsDatabaseRef.child("Post").child(userId).push().setValue(postDictionary);
调用push()。要了解有关这些ID的更多信息,请参阅。hey arron,您可以使用firebase updateChildren()方法进行数据库引用。我尝试过这样使用,但当前信息仍在被productsDatabaseRef.child(“Post”).child(userId)。updateChildren(postDictionary);显示你的firebase数据库nodeI编辑了我的问题,请检查上面。这就是它的结构。我基本上是在尝试允许用户发布大量帖子,并将信息保存在UID下。嗯,我看到在postDictionary映射中,您包含了数据库节点中的所有内容。这就是为什么所有这些都被替换的原因。Y arron,您可以使用firebase updateChildren()方法进行数据库引用。我尝试过这样使用,但当前信息仍然被替换为productsDatabaseRef.child(“Post”).child(userId)。updateChildren(postDictionary);显示你的firebase数据库nodeI编辑了我的问题,请检查上面。这就是它的结构。我基本上是在尝试允许用户发布大量帖子,并将信息保存在UID下。嗯,我看到在postDictionary映射中,您包含了数据库节点中的所有内容。这就是为什么所有的都是replacingHi,我实现了时间戳,我的数据库看起来就像你描述的,如果我想用新添加的时间戳访问post节点,我该怎么做?我已经添加了上面的信息以从firebase检索数据!!在上面的代码中,我们在userId中做了一个循环。第一个循环需要第一个日期,然后进入第二个循环并检索信息。然后再次进入第一个循环,依此类推。您好,我实现了时间戳,我的数据库如您所述,如果我想使用新添加的时间戳访问post节点,我将如何操作?我已添加上述信息以从firebase检索数据!!在上面的代码中,我们在userId中做了一个循环。第一个循环需要第一个日期,然后进入第二个循环并检索信息。然后再次进入第一个循环,依此类推。