简体   繁体   中英

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

I'm still new at this. Used a push() when storing data, but now am having trouble retrieving it, I can only retrieve the information that was stored the first time before using the push(). Here is the code I used to store the information.

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

This is the code that am using to retrieve information. So this only gets the information that has been stored for the first time.

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

        }
    });

}

If I read your code correctly you have this data structure:

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

There is no way to query for the idnumber property across all users as your code is trying to do, as queries on the Realtime Database can only contain one level of unknown keys and you have two ( $idnumber and $pushkey ). Also see: Firebase Query Double Nested

But since you also have $idnumber as the key under Users , you can load that node with a query and then loop over its $pushkey child nodes with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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