gpt4 book ai didi

java - 如何在 Firebase 实时数据库中的特定子树中推送值

转载 作者:行者123 更新时间:2023-11-29 15:33:28 26 4
gpt4 key购买 nike

我正在尝试将带有名称的 url 上传到 Firebase 数据库中的特定树子。

代码:

public void UploadData(View view){

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/gif");
startActivityForResult(intent,ImageBack);

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

Folder = FirebaseStorage.getInstance().getReference().child("ImageFolder");
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
source2 = textView.getText().toString();

if(requestCode == SELECT_VIDEO){
if(resultCode == RESULT_OK){
Uri ImageData = data.getData();

final StorageReference Imagename = Folder.child("image" + ImageData.getLastPathSegment());

Imagename.putFile(ImageData).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

// Toast.makeText(realupload.this, "Uploaded", Toast.LENGTH_SHORT).show();

Imagename.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {

DatabaseReference imageStore = FirebaseDatabase.getInstance().getReference().child("main");
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put(source2, String.valueOf(uri));

imageStore.setValue(hashMap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(realupload.this, "Finally Completed", Toast.LENGTH_SHORT).show();
}
});

}
});
}
});
}
}
}

子树“main”是我要为其添加值的子树。 enter image description here

目前,当我使用它时,它将覆盖我在 Firebase 数据库中“主要”子树中的所有值。我该如何更改它,以便它不会覆盖我的“主”子树,而只是在其下添加另一个具有值的名称。

最佳答案

How do I change this so that it doesn't override my "main" child tree, but just add another name with value under it.

您可以使用 updateChildren() 方法:

    Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("food","all" );
mDatabase.updateChildren(childUpdates);

更多信息在这里:

https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields

关于java - 如何在 Firebase 实时数据库中的特定子树中推送值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59207051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com