java - 从 firebase 数据库读取数据到自定义 listView

标签 java android firebase-realtime-database

enter image description here我正在尝试读取插入到 firebase 数据库中的数据,我想在自定义 listView 中显示数据。我正在使用这种方法,但似乎我错过了一些东西。 我无法将数据插入我的数组列表。 更新我插入了值,现在它给了我

attempt to invoke a virtual method on a null object reference on my get methods

private void showData(DataSnapshot dataSnapshot) {
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        PatientInformation patientInfo = new PatientInformation();
        patientInfo.setName(ds.child(userID).getValue(PatientInformation.class).getName());
        patientInfo.setAge(ds.child(userID).getValue(PatientInformation.class).getAge());
        patientInfo.setDate(ds.child(userID).getValue(PatientInformation.class).getDate());
        ArrayList<PatientInformation> arrayList = new ArrayList<>();
        arrayList.add(patientInfo);
        PatientInformationAdapter pAdapter = new PatientInformationAdapter(this, arrayList);
        listView.setAdapter(pAdapter);
    }

最佳答案

您的 arrayList 是 PatientInformation 类型,因此我认为您可以添加也是 PatientInformation 类型的 PatientInfo。

private void showData(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
    PatientInformation patientInfo = new PatientInformation();
    patientInfo.setName(ds.child(userID).getValue(PatientInformation.class).getName());
    patientInfo.setAge(ds.child(userID).getValue(PatientInformation.class).getAge());
    patientInfo.setDate(ds.child(userID).getValue(PatientInformation.class).getDate());
    ArrayList<PatientInformation> arrayList = new ArrayList<>();
    arrayList.add(patientInfo);

    PatientInformationAdapter pAdapter = new PatientInformationAdapter(this, arrayList);
    listView.setAdapter(pAdapter);
}

关于java - 从 firebase 数据库读取数据到自定义 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60102451/

相关文章:

ios - Firebase 查询返回 Null

java - Amazon SNS 使用 Java 推送主题

使用tomcat服务器部署Java birt报告

Swift - 数据库中的 Firebase 搜索

java - 在 Google Android 中以编程方式连接到 WiFi?

android - 我的真实设备上的 SQLite 数据库在哪里?

android - 我无法显示来自 firebase 数据库的实际格式数据

java - 将文件通过 FTP 传输到服务器,但结果以零字节大小到达

java - 在Android中使用restTemplate.getForObject反序列化JSON对象的正确方法

android - 支付宝 "networkError"