简体   繁体   English

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

[英]How to push value in specific child tree in Firebase Realtime Database

I am trying to upload a url with a name to a specific tree child in Firebase Database.我正在尝试将带有名称的 url 上传到 Firebase 数据库中的特定树子节点。

Code :代码 :

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();
                                    }
                                });

                            }
                        });
                    }
                });
            }
        }
    }

The child tree "main" is the child tree that I want to add values to.子树“main”是我想要添加值的子树。 在此处输入图片说明

Currently when I use this, it will override my all my values in my "main" child tree in Firebase Database.目前,当我使用它时,它将覆盖我在 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.如何更改它以使其不会覆盖我的“主”子树,而只是在其下添加另一个带有值的名称。

How do I change this so that it doesn't override my "main" child tree, but just add another name with value under it.如何更改它以使其不会覆盖我的“主”子树,而只是在其下添加另一个带有值的名称。

You can use the updateChildren() method:您可以使用updateChildren()方法:

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

More information here:更多信息在这里:

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

simply you just need to update value.只是你只需要更新值。 for update value use this code imageStore. updateChildren(hashMap)对于更新值,请使用此代码imageStore. updateChildren(hashMap) imageStore. updateChildren(hashMap) to update value imageStore. updateChildren(hashMap)更新值

Or You can replace below code.或者您可以替换以下代码。

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. updateChildren(hashMap).addOnSuccessListener(new OnSuccessListener<Void>() {
                                    @Override
                                    public void onSuccess(Void aVoid) {
                             Toast.makeText(realupload.this, "Finally Completed", Toast.LENGTH_SHORT).show();
                                    }
                                });

                            }
                        });
                    }
                });
            }
        }
    }

for more information https://stackoverflow.com/a/41296760/9315408更多信息https://stackoverflow.com/a/41296760/9315408

You need to find a specif key to update.您需要找到要更新的特定密钥。

ie imageStore.child("cinta").setValue("value")imageStore.child("cinta").setValue("value")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM