【问题标题】:How create nested child node in firebase realtime database android如何在firebase实时数据库android中创建嵌套子节点
【发布时间】:2020-11-26 14:15:09
【问题描述】:

您好,我正在尝试制作嵌套子节点,但我不知道如何

这是我的数据

我想做假银行

就像在电话下我想添加另一个名为捐赠的数据,在其中我想添加名为历史的节点,就像任何时候捐赠者捐赠它应该存储在历史中一样 有谁知道我该怎么做???

这是我的代码

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_donor__registration);
    doname=(EditText)findViewById(R.id.editname);
    dopass=(EditText)findViewById(R.id.editpass);
    docon=(EditText)findViewById(R.id.editcon);
    dophone=(EditText)findViewById(R.id.editphone);
    doemail=(EditText)findViewById(R.id.editemail);
    btn_register=(Button)findViewById(R.id.button2);
    myref1= FirebaseDatabase.getInstance().getReference("Donors");
    firebaseAuth =FirebaseAuth.getInstance();//d


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

            String do_name=doname.getText().toString();
            String do_pass=dopass.getText().toString();
            String do_con=docon.getText().toString();
            String do_phone=dophone.getText().toString();
            String do_email=doemail.getText().toString();

            //validation

            if(do_name.equals(""))
                doname.setError("User name Should not be empty");
            else if(do_pass.equals(""))
                dopass.setError("Password Should not be empty");
            else if(do_con.equals(""))
                docon.setError("Confirm Password Should not be empty");
            else if(do_phone.equals(""))
                dophone.setError("Phone Should not be empty");
            else if(do_email.equals(""))
                doemail.setError("Email Should not be empty");
            else if(!do_pass.equals(do_con))
                docon.setError("Password and Confirm password should match");
            else if(do_pass.length()<8)
                dopass.setError("Password Should be at least 8 charaters");
            else if(do_phone.length()<8)
                dophone.setError("Phone number should be  8 digits");
            else if(!do_phone.startsWith("7")&& !do_phone.startsWith("9"))
                dophone.setError("Should Start with 7 or 9");
              else if(!do_email.matches(emailpattern))
                doemail.setError("Email Should be correct");



                //else

            else {


                //


                myref1.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        int flag = 0;
                        for (DataSnapshot d : snapshot.getChildren()) {
                            Donors ado = d.getValue(Donors.class);
                            if (ado.d_email.equalsIgnoreCase(do_email)) {
                                flag = 1;
                                Toast.makeText(Donor_Registration.this, "Donor already exists", Toast.LENGTH_LONG).show();
                            }
                        }



                        if (flag == 0) {

                            //
                            firebaseAuth.createUserWithEmailAndPassword(doemail.getText().toString(),dopass.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    if(task.isSuccessful()){
                                        Toast.makeText(Donor_Registration.this,"Donor registered",Toast.LENGTH_LONG).show();



                                    }

                                  //  else
                                   // {
                                       // Toast.makeText(Donor_Registration.this,task.getException().getMessage(),Toast.LENGTH_LONG).show();
                                  //  }

                                }
                            });


                           Donors ado = new Donors(do_name, do_pass, do_phone, do_email);
                           String id = myref1.push().getKey();
                           myref1.child(id).setValue(ado);
                          Toast.makeText(Donor_Registration.this, "Donor registered", Toast.LENGTH_SHORT).show();







                        }


                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });

            }

        }



    });














}

【问题讨论】:

  • 可以直接使用子节点
  • 是的,但是捐赠者会多次捐赠我想要一些帮助我的东西......就像我希望每次捐赠者捐赠时都必须将其存储在历史记录中
  • 就像我有(捐赠者)在里面,我应该在里面有(捐赠者信息)他的信息,并且每次捐赠者捐赠时我都希望里面有(历史),它必须存储在那里
  • 你想在d_phone中添加数据对吗?
  • 不,我想添加一些叫做捐赠或历史的东西

标签: android firebase firebase-realtime-database


【解决方案1】:

您的代码正在设置整个施主节点。

FirebaseReference donorsNode = FirebaseDatabase.getInstance().getReference("Donors");
FirebaseReference donorNode = donorsNode.child(donorId);
donorNode.setValue(entireDonorValue);

要使用捐赠更新它,只需在现有捐赠节点内设置一个子节点。

FirebaseReference donorsNode = FirebaseDatabase.getInstance().getReference("Donors");
FirebaseReference donorNode = donorsNode.child(donorId);
FirebaseReference donationsNode = donorNode.child("Donations");
FirebaseReference donationNode = donationsNode.child(donationId);
donationNode.setValue(donationValue);

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 2018-05-05
    • 2021-09-24
    • 1970-01-01
    • 2020-11-27
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多