簡體   English   中英

如何在 firebase 實時數據庫 android 中創建嵌套子節點

[英]How create nested child node in firebase realtime database android

您好我正在嘗試制作嵌套子節點,但我不知道如何

這是我的數據

在此處輸入圖像描述

我想做假銀行

就像在電話下我想添加另一個名為捐贈的數據,在其中我想添加一個名為歷史的節點,就像任何時候捐贈者捐贈的錢一樣,它應該存儲在歷史中任何人都知道我該怎么做???

這是我的代碼

 @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) {

                    }
                });

            }

        }



    });














}

您的代碼正在設置整個施主節點。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM