android 只有用户节点数据库的新用户正在检索,但我想显示当前登录用户的信息

5t7ly7z5  于 2023-05-05  发布在  Android
关注(0)|答案(1)|浏览(103)

这是我的代码来检索当前登录用户,但它只给予我新注册的用户或身份验证列表This is the authentication of my firebase顶部的证书
This is the exported database json和它总是显示kahdb@gmail.com(信息的Noway)用户,但我使用不同的用户凭据。

public void showAllUserData() {
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User");
    FirebaseAuth auth = FirebaseAuth.getInstance();
    FirebaseUser user = auth.getCurrentUser();
    String userKey = user.getUid();
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.child(userKey).exists()) {
                    String nameString = dataSnapshot.child(userKey).child("userName").getValue(String.class);
                    String studNumString = dataSnapshot.child(userKey).child("userStudNo").getValue(String.class);
                    String emailString = dataSnapshot.child(userKey).child("userEmail").getValue(String.class);
                    String passString = dataSnapshot.child(userKey).child("userPass").getValue(String.class);

                    editname.setText(nameString);
                    editstudentNo.setText(studNumString);
                    editEmail.setText(emailString);
                    editPassword.setText(passString);
                    nameLabel.setText(nameString);
                    studentNoLabel.setText(studNumString);
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.e("DatabaseError", "Error reading from database: " + error.getMessage());
            }
        });
    }

这是我完整的配置文件代码,请帮助我修改这个,以获得所需的输出。我想显示此Michelle user info,但只有Alvarez正在显示

package com.example.practice;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.practice.databinding.ActivityProfileBinding;
import com.example.practice.databinding.ActivityScannerBinding;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

public class Profile extends Drawable {

    TextView nameLabel, studentNoLabel, editname, editstudentNo, editEmail, editPassword;
    Button update;
    ActivityProfileBinding activityProfileBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityProfileBinding = ActivityProfileBinding.inflate(getLayoutInflater());
        setContentView(activityProfileBinding.getRoot());
        allocateActivityTitle("Attendance Scanner");

        editname = findViewById(R.id.editTextName);
        editstudentNo = findViewById(R.id.editTextStudentNo);
        editEmail = findViewById(R.id.editTextEmail);
        editPassword = findViewById(R.id.editTextPassword);

        nameLabel = findViewById(R.id.nameLabel);
        studentNoLabel = findViewById(R.id.studentNoLabel);
        update = findViewById(R.id.update);
        
        showAllUserData();
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 passUserData();
            }
        });
    }

    public void showAllUserData() {
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User");
        FirebaseAuth auth = FirebaseAuth.getInstance();
        FirebaseUser user = auth.getCurrentUser();
        String userKey = user.getUid();
            ref.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.child(userKey).exists()) {
                        String nameString = dataSnapshot.child(userKey).child("userName").getValue(String.class);
                        String studNumString = dataSnapshot.child(userKey).child("userStudNo").getValue(String.class);
                        String emailString = dataSnapshot.child(userKey).child("userEmail").getValue(String.class);
                        String passString = dataSnapshot.child(userKey).child("userPass").getValue(String.class);

                        editname.setText(nameString);
                        editstudentNo.setText(studNumString);
                        editEmail.setText(emailString);
                        editPassword.setText(passString);
                        nameLabel.setText(nameString);
                        studentNoLabel.setText(studNumString);
                    }
                }
                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                    Log.e("DatabaseError", "Error reading from database: " + error.getMessage());
                }
            });
        }
    public void passUserData() {
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User");
        FirebaseAuth auth = FirebaseAuth.getInstance();
        FirebaseUser user = auth.getCurrentUser();
        if (user != null) {
            String userKey = user.getUid();
            ref.child(userKey).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    if (dataSnapshot.exists()) {
                        String nameDB = dataSnapshot.child("userName").getValue(String.class);
                        String emailDB = dataSnapshot.child("userEmail").getValue(String.class);
                        String studentNoDB = dataSnapshot.child("userStudNo").getValue(String.class);
                        String pass = dataSnapshot.child("userPass").getValue(String.class);

                        Intent intent = new Intent(Profile.this, editProfile.class);

                        intent.putExtra("userName", nameDB);
                        intent.putExtra("userEmail", emailDB);
                        intent.putExtra("userStudNo", studentNoDB);
                        intent.putExtra("userPass", pass);
                        startActivity(intent);
                    }

                }

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

                }
            });
        }
    }
    }
bf1o4zei

bf1o4zei1#

您正在检索一个用户列表,然后使用setText,它将显示您在此列表中检索到的最后一个用户。您需要将数据库模式更改为以下内容:

users
  <user-id>
       userName: "michy"
       userStudNo : 1

然后您可以执行以下操作:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User");
 FirebaseAuth auth = FirebaseAuth.getInstance();
 FirebaseUser user = auth.getCurrentUser();
 String userKey = user.getUid();

    ref.child(userKey).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String nameString = dataSnapshot.child("userName").getValue(String.class);
                String studNumString = dataSnapshot.child("userStudNo").getValue(String.class);

                name.setText("" + nameString);
                studNum.setText("" + studNumString);
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
}}

您需要获取userId,然后将其作为参数传递给child(),然后不再需要循环,您可以直接获取当前用户的信息。

相关问题