繁体   English   中英

Firebase 实时 - 如何检索由 push() 生成的数据?

[英]Firebase Realtime - How do I retrieve data that has been generated by the push()?

我对此还是陌生的。 存储数据时使用了 push(),但现在无法检索它,我只能检索使用 push() 之前第一次存储的信息。 这是我用来存储信息的代码。

regBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        rootnode = FirebaseDatabase.getInstance();
        reference = rootnode.getReference("Users");

        //get all the values
        String firstName = fname.getEditText().getText().toString();
        String lastName = lname.getEditText().getText().toString();
        String idnumber = idnum.getEditText().getText().toString();
        String dateofBirth = dateob.getEditText().getText().toString();
        String email = e_mail.getEditText().getText().toString();
        String phoneNumber = pnum.getEditText().getText().toString();
        String nextofKin = kin.getEditText().getText().toString();
        String nextofKinNumber = nkin.getEditText().getText().toString();``
        String password = pass_word.getEditText().getText().toString();

        if (!validateFirstName() | !validateLastName() | !validateIdNumber() | !validateDateofBirth() | !validateEmaiL() | !validatePhoneNumber() | !validateNextOFKin() | !validateNextOfKinContact() | !validatePassWord()){
            return;
        }
        else {
            Toast.makeText(getApplicationContext(),"registered",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this,fbase.class);
            startActivity(intent);
        }

        Userhelperclass helperclass = new Userhelperclass(firstName,lastName,idnumber,dateofBirth,email,phoneNumber,nextofKin,nextofKinNumber,password);
        reference.child(idnumber).push().setValue(helperclass);
    }
});

这是用于检索信息的代码。 所以这只会获取第一次存储的信息。

private void isUser() {

    String userEnteredIdNumber = idnum.getEditText().getText().toString().trim();
    String userEnteredPassword = pass_word.getEditText().getText().toString().trim();
    String userEnteredFirstName = fname.getEditText().getText().toString().trim();

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
    Query checkUser = reference.orderByChild("idnumber").equalTo(userEnteredIdNumber);

    checkUser.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {

            if (snapshot.exists()){
                idnum.setError(null);
                idnum.setErrorEnabled(false);

                String passwordFromDb = snapshot.child(userEnteredIdNumber).child("password").getValue(String.class);
                String firstnameFromDb = snapshot.child(userEnteredIdNumber).child("firstName").getValue(String.class);

                idnum.setError(null);
                idnum.setErrorEnabled(false);


                if (passwordFromDb.equals(userEnteredPassword) && firstnameFromDb.equals(userEnteredFirstName)){

                    String lastnamesFromDb = snapshot.child(userEnteredIdNumber).child("lastName").getValue(String.class);
                    String idnumbersFromDb = snapshot.child(userEnteredIdNumber).child("idnumber").getValue(String.class);
                    String birthsFromDb = snapshot.child(userEnteredIdNumber).child("dateofBirth").getValue(String.class);
                    String mailsFromDb = snapshot.child(userEnteredIdNumber).child("email").getValue(String.class);
                    String contactsFromDb = snapshot.child(userEnteredIdNumber).child("phoneNumber").getValue(String.class);
                    String kinsFromDb = snapshot.child(userEnteredIdNumber).child("nextofKin").getValue(String.class);
                    String kinsnumberFromDb = snapshot.child(userEnteredIdNumber).child("nextofKinNumber").getValue(String.class);

                    fname.getEditText().setText(firstnameFromDb);
                    lname.getEditText().setText(lastnamesFromDb);
                    idnum.getEditText().setText(idnumbersFromDb);
                    dateob.getEditText().setText(birthsFromDb);
                    e_mail.getEditText().setText(mailsFromDb);
                    pnum.getEditText().setText(contactsFromDb);
                    kin.getEditText().setText(kinsFromDb);
                    nkin.getEditText().setText(kinsnumberFromDb);
                    pass_word.getEditText().setText(passwordFromDb);

                }
                else if(!passwordFromDb.equals(userEnteredPassword)){

                    pass_word.setError("wrong password");

            }
                else {
                    fname.setError("Wrong name");
                }

            }
            else {
                idnum.setError("No such user exists");
                idnum.requestFocus();
            }
        }


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

        }
    });

}

如果我正确阅读了您的代码,您将拥有以下数据结构:

Users: {
  $idnumber: {
    $pushkey: {
      idnumber: $idnumber
    }
  }
}

无法像您的代码尝试那样查询所有用户的idnumber属性,因为对实时数据库的查询只能包含一级未知键,而您有两个( $idnumber idnumber 和$pushkey )。 另见: Firebase 查询双重嵌套

但是由于您也有$idnumber作为Users下的键,您可以使用查询加载该节点,然后使用以下命令遍历其$pushkey子节点:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
Query theUser = reference.child(userEnteredIdNumber);

theUser.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
        for (DataSnapshot pushSnapshot: snapshot.getChildren()) { // 👈 Loop over push keys
            String passwordFromDb = pushSnapshot.child("password").getValue(String.class);
            ...
         }
    }


    @Override
    public void onCancelled(@NonNull @NotNull DatabaseError error) {
        throw error.toException(); // 👈 Never ignore errors
    }
});

暂无
暂无

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

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