【问题标题】:Add authenticated Firebase users to Listview将经过身份验证的 Firebase 用户添加到 Listview
【发布时间】:2024-05-20 09:15:01
【问题描述】:

我想将经过 Firebase 身份验证的用户(在输入正确的用户名和密码后)添加到 ListView。为了更好地理解这里是我的应用程序的流程。

Here is the image of first layout when app starts

1- 当用户点击添加患者按钮时,应用程序会重定向到另一个活动,用户必须输入正确的电子邮件和密码

2- 成功登录后,应用再次打开上一个活动(添加患者活动)并将登录用户添加到 ListView。这是添加患者的xml

<TextView
    android:layout_width="match_parent"
    android:layout_height="59dp"
    android:layout_alignParentTop="true"
    android:id="@+id/addpatient"
    android:gravity="center"
    android:drawableLeft="@drawable/ic_add_circle_outline_black_48dp"
    android:drawablePadding="-170dp"
    android:textStyle="bold"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    android:text="Add Patient"
    android:layout_marginTop="57dp" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/addpatient"
    android:background="#000" />

<ListView
    android:id="@+id/listpatient"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_weight="1"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:longClickable="true"
    android:layout_marginTop="119dp"
    android:layout_alignParentBottom="true">
</ListView>

请注意,我不想从 Firebase 数据库中获取用户列表,我使用的是 firebase 身份验证,而不是 firebase 数据库。这是添加患者活动的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_patient);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
    setSupportActionBar(toolbar);

    auth = FirebaseAuth.getInstance();

    addPatient = (TextView)findViewById(R.id.addpatient);
    listPatient = (ListView)findViewById(R.id.listpatient);
    arrayList = new ArrayList<String>();
    if(patient == null)
    {
        Toast.makeText(getApplicationContext(),"No user found",Toast.LENGTH_SHORT).show();
    }
    else
    {
        patient = FirebaseAuth.getInstance().getCurrentUser();
        arrayList.add(String.valueOf(patient));
    }
    adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
    listPatient.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    addPatient.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent it = new Intent(AddPatient.this,DoctorPanel_Signin.class);
            startActivity(it);
        }
    });

问题是列表视图没有显示任何数据。如果我删除 IF 语句,应用程序会通过尝试在空指针引用上调用虚拟方法来崩溃。我在想的另一件事是,如果我想添加 3 个用户,那么 firebaseaut.getinstance.getcurrentuser 将始终返回 1 个用户,并且会一次又一次地覆盖以前的用户,请也考虑一下。

【问题讨论】:

    标签: android listview firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    发生这种情况是因为您在创建患者对象之前验证是否patient == null。因此,换句话说,您的 patient object 始终为空。要解决这个问题,您需要像这样更改 if 语句的逻辑:

    FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
            if (firebaseUser != null) {
                Log.d("TAG", "You are signed in Firebase!");
                arrayList.add(String.valueOf(patient));
            } else {
                Log.d("TAG", "You are signed out from Firebase!");
            }
        }
    };
    

    您的数据库应如下所示:

    Firebase-root
         |
         --- Users
               |
               --- userUid1
               |      |
               |      --- signedInSuccessfully: false
               |
               --- userUid2
                      |
                      --- signedInSuccessfully: true //changes after signed in successfully
    

    【讨论】:

    • 崩溃了..我把你的代码..这是错误.. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zohai.healthapp/com.example.zohai .healthapp.DoctorPanel.AddPatient}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String com.google.firebase.auth.FirebaseUser.getEmail()”
    • 此活动是启动器活动,因此由于我尚未登录,因此耐心将始终为空
    • 患者对象在登录后将获得用户ID
    • 请看我更新的答案。这是完整的代码。
    • FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();这将仅返回当前登录的用户。如果我想用多个用户填充列表视图而不是我应该做什么而不是 firebaseAuth.getCurrentuser();因为如果我再次使用不同的凭据登录,它会覆盖列表视图中的前一个用户
    最近更新 更多