简体   繁体   English

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

[英]How do i get a push() key in and store the key at the same time

this is my current coding, i want to store the push() key by the time i clicked submit.这是我当前的编码,我想在单击提交时存储 push() 键。 The tree would be uid > push key > propertyid(pushkey),name and so on.该树将是 uid > push key > propertyid(pushkey),name 等等。

current database当前数据库

coding are for android application.编码用于android应用程序。 thanks谢谢

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

Inside the onClick() , do the following: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();
                        }
                    }
                });

You need to store the push() key in variable and then you can use it as an argument inside the method child() , and pass it through activities using Intent .您需要将push()键存储在变量中,然后您可以将其用作方法child()的参数,并使用Intent通过活动传递它。

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

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