从 firebase 检索数据后获取空白 ListView

Getting a Blank ListView after retrieving data from firebase

我在 运行 上得到一个空白的 listView 这个 code.I 已经检查了数组的内容,方法是将它们显示在 listView.Changing 底部的文本视图中以线性布局效果不佳。 这是我第一次使用 listView,所以请原谅我的愚蠢问题。

XML 代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_background"
    tools:context=".current_month">

    <ListView
        android:id="@+id/barcode_detail"
        android:layout_width="374dp"
        android:layout_height="403dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.486"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.277" />

    <TextView
        android:id="@+id/title_tv"
        android:layout_width="299dp"
        android:layout_height="42dp"
        android:layout_marginStart="141dp"
        android:layout_marginEnd="141dp"
        android:fontFamily="sans-serif-black"
        android:textAlignment="center"
        android:textColor="#F7F3F3"
        android:textSize="24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.037" />

    <TextView
        android:id="@+id/resultView"
        android:layout_width="245dp"
        android:layout_height="143dp"
        android:layout_marginStart="29dp"
        android:layout_marginTop="29dp"
        android:layout_marginEnd="65dp"
        android:layout_marginBottom="65dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/bar_code_scan"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/barcode_detail" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/bar_code_scan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:clickable="true"
        android:focusable="true"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/fruit_scan"
        app:layout_constraintEnd_toEndOf="@+id/fruit_scan"
        app:srcCompat="@drawable/ic_barcode_scan" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/scanButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="339dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        android:contentDescription="TODO"
        android:focusable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/ic_baseline_add_24" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fruit_scan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:clickable="true"
        android:focusable="true"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/scanButton"
        app:layout_constraintEnd_toEndOf="@+id/scanButton"
        app:srcCompat="@drawable/ic_vegetable_scan" />


</androidx.constraintlayout.widget.ConstraintLayout>

JAVA 代码(oncreate 函数的一个子集)

final ArrayList<String> dat = new ArrayList<>();
        final FirebaseUser users = firebaseAuth.getCurrentUser();
        String result = users.getEmail().replace(".","");
        DatabaseReference databaseReference2 = FirebaseDatabase.getInstance().getReference().child("Users").child(result).child("January").child("Product");
        databaseReference2.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                    dat.add(snapshot.child("productname").getValue().toString());
                }
            }

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

            }
        });
        ArrayAdapter arrayAdapter = new ArrayAdapter(current_month.this, android.R.layout.simple_list_item_1,dat);
        barcode_detail.setAdapter(arrayAdapter);

您的 arrayAdapter 未收到列表更改通知。

将项目添加到列表后,您应该调用 arrayAdapter.notifyDataSetChanged();

您的代码应如下所示:

final ArrayList<String> dat = new ArrayList<>();
        final FirebaseUser users = firebaseAuth.getCurrentUser();
        String result = users.getEmail().replace(".","");
        ArrayAdapter arrayAdapter = new ArrayAdapter(current_month.this, android.R.layout.simple_list_item_1,dat);
        barcode_detail.setAdapter(arrayAdapter);
        DatabaseReference databaseReference2 = FirebaseDatabase.getInstance().getReference().child("Users").child(result).child("January").child("Product");
        databaseReference2.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                    dat.add(snapshot.child("productname").getValue().toString());
                }
                arrayAdapter.notifyDataSetChanged();
            }

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

            }
        });