【发布时间】: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