我如何获得push()密钥并同时存储密钥

时间:2019-03-21 07:36:08

标签: android firebase firebase-realtime-database

这是我当前的编码,我想在单击提交时存储push()键。 树将是uid>按键> propertyid(pushkey),名称等。

current database

编码适用于android应用程序。 谢谢

submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
            final DatabaseReference reference  = FirebaseDatabase.getInstance().getReference("Property");

            String OwnerID = firebaseUser.getUid();
            String name = property_name.getText().toString();
            String address = property_address.getText().toString();
            String postal = property_postal.getText().toString();
            String state = property_state.getText().toString();
            String detail = property_detail.getText().toString();
            String imageUrl = "default";

            Advertise adv = new Advertise(name,address,postal,state,detail,imageUrl,OwnerID);

            if (TextUtils.isEmpty(name)){
                Toast.makeText(getActivity(),"Enter Property Name!",Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(address)){
                Toast.makeText(getActivity(),"Enter Property Address!",Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(postal)){
                Toast.makeText(getActivity(),"Enter Property Postal Code!",Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(detail)){
                Toast.makeText(getActivity(),"Enter Property Details!",Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(state)){
                Toast.makeText(getActivity(),"Enter Property State/Province!",Toast.LENGTH_SHORT).show();
                return;
            }

            reference.child(firebaseUser.getUid()).push().setValue(adv)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if(task.isSuccessful()){
                                Toast.makeText(getActivity(),"Advertisement published",Toast.LENGTH_LONG).show();

                            }else {
                                Toast.makeText(getActivity(),"Something went wrong!",Toast.LENGTH_LONG).show();
                            }
                        }
                    });
        }
    });

1 个答案:

答案 0 :(得分:0)

onClick()内,执行以下操作:

String key = reference.child(firebaseUser.getUid()).push().getKey();
reference.child(firebaseUser.getUid()).child(key).setValue(adv)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(getActivity(),"Advertisement published",Toast.LENGTH_LONG).show();

                        }else {
                            Toast.makeText(getActivity(),"Something went wrong!",Toast.LENGTH_LONG).show();
                        }
                    }
                });

您需要将push()键存储在变量中,然后可以将其用作方法child()中的参数,并使用Intent将其传递给活动。