java - Firebase Android ListView 未显示

标签 java android firebase listview firebase-realtime-database

我正在尝试在用户登录后在主菜单屏幕的 ListView 中显示来自 firebase 实时数据库的数据。该应用程序正在运行但不显示数据。

这是我数据库中的数据

enter image description here

现在是代码。

MainMenu.java 此函数在 OnCreate() 上调用。

 public void makeItem ()
    {
        lv = findViewById(R.id.listView);
        db = FirebaseDatabase.getInstance().getReference();
        helper = new FirebaseHelper(db);
        adapter= new AdapterItem(this,helper.retrive());
        lv.setAdapter(adapter);

    } 

自定义列表适配器.java

public class CustomListAdapter{

    private String ItemName;
    private String Quantity;
    private String SerialNo;
    private String SupplierName;
    private String SupplierEmail;
    private String SupplierPhone;


    public CustomListAdapter(){

    }

    public CustomListAdapter (String ItemName,String Quantity,String SerialNo,String SupplierName,String SupplierEmail,String SupplierPhone)
    {
        this.ItemName = ItemName;
        this.Quantity = Quantity;
        this.SerialNo = SerialNo;
        this.SupplierName = SupplierName;
        this.SupplierEmail = SupplierEmail;
        this.SupplierPhone = SupplierPhone;
    }

    public void setItemName (String ItemName)
    {
        this.ItemName = ItemName;
    }

    public String getItemName ()
    {
        return ItemName;
    }

    public void setQuantity (String Quantity)
    {
        this.Quantity = Quantity;
    }


    public String getQuantity ()
    {
        return Quantity;
    }

    public void setSerialNo (String SerialNo)
    {
        this.SerialNo = SerialNo;
    }


    public String getSerialNo ()
    {
        return SerialNo;
    }

    public void setSupplierName (String SupplierName)
    {
        this.SupplierName = SupplierName;
    }

    public String getSupplierName()
    {
        return SupplierName;
    }

    public void setSupplierEmail (String SupplierEmail)
    {
        this.SupplierEmail = SupplierEmail;
    }

    public String getSupplierEmail() {
        return SupplierEmail;
    }

    public void setSupplierPhone (String SupplierPhone)
    {
        this.SupplierPhone = SupplierPhone;
    }

    public String getSupplierPhone() {
        return SupplierPhone;
    }
}

AdapterItem.java

public class AdapterItem extends BaseAdapter {
    Context c;
    ArrayList<CustomListAdapter> customListAdapters;

    public AdapterItem(Context c, ArrayList<CustomListAdapter> customListAdapters) {
        this.c = c;
        this.customListAdapters = customListAdapters;
    }

    @Override
    public int getCount() {
        return customListAdapters.size();
    }

    @Override
    public Object getItem(int position) {
        return customListAdapters.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null)
        {
            convertView=LayoutInflater.from(c).inflate(R.layout.content_main_menu_list,parent,false);
        }
        TextView ItemName = convertView.findViewById(R.id.name);
        TextView SerialNo = convertView.findViewById(R.id.serialNo);
        TextView SupplierName = convertView.findViewById(R.id.supplierName);
        TextView amount = convertView.findViewById(R.id.amount);

        final CustomListAdapter CLA = (CustomListAdapter) this.getItem(position);

        ItemName.setText(CLA.getItemName());
        SerialNo.setText(CLA.getSerialNo());
        SupplierName.setText(CLA.getSupplierName());
        amount.setText(CLA.getQuantity());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(c,CLA.getItemName(),Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }
}

content_main_menu.xml

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main_menu">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp" />

content_main_menu_list.xml 是我为要显示的每个数据集创建的自定义布局。

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:text="TextView"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

        <TextView
            android:id="@+id/serialNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/supplierName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="TextView"
            app:layout_constraintTop_toBottomOf="@+id/serialNo" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="23dp"
            android:layout_marginBottom="8dp"

            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#0171B0"
            app:layout_constraintBottom_toTopOf="@+id/serialNo" />
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

最佳答案

您的代码中的问题在于您在 CustomListAdapter 类中有一个名为 ItemName 的字段,但您使用的是名为 getItemName() 的 getter ,这是不正确的,因为 Firebase 在数据库中查找名为 itemName 而不是 ItemName 的字段。看到小写字母 i 与大写字母 I 了吗?

有两种方法可以解决这个问题。第一个是通过根据 Java Naming Conventions 重命名字段来更改您的模型类.所以你的模型类应该是这样的:

public class CustomListAdapter {
    private String itemName, quantity, serialNo, supplierName, supplierEmail, supplierPhone;

    public CustomListAdapter() {}

    public CustomListAdapter(String itemName, String quantity, String serialNo, String supplierName, String supplierEmail, String supplierPhone) {
        this.itemName = itemName;
        this.quantity = quantity;
        this.serialNo = serialNo;
        this.supplierName = supplierName;
        this.supplierEmail = supplierEmail;
        this.supplierPhone = supplierPhone;
    }

    public String getItemName() { return itemName; }
    public String getQuantity() { return quantity; }
    public String getSerialNo() { return serialNo; }
    public String getSupplierName() { return supplierName; }
    public String getSupplierEmail() { return supplierEmail; }
    public String getSupplierPhone() { return supplierPhone; }
}

看这个例子,有private字段和public getters。还有一个更简单的解决方案,直接在公共(public)字段上设置值,如下所示:

public class CustomListAdapter {
    public String itemName, quantity, serialNo, supplierName, supplierEmail, supplierPhone;
}

现在只需删除当前数据并使用正确的名称重新添加。此解决方案仅在您处于测试阶段时才有效。

还有第二种方式,就是使用annotations。因此,如果您更喜欢使用私有(private)字段和公共(public) getter,则应该使用 PropertyName仅在 setter/getter 前面注释。所以您的 CustomListAdapter 类应该如下所示:

public class CustomListAdapter {
    private String ItemName;
    private String Quantity;
    private String SerialNo;
    private String SupplierName;
    private String SupplierEmail;
    private String SupplierPhone;

    public CustomListAdapter() {}

    public CustomListAdapter(String itemName, String quantity, String serialNo, String supplierName, String supplierEmail, String supplierPhone) {
        ItemName = itemName;
        Quantity = quantity;
        SerialNo = serialNo;
        SupplierName = supplierName;
        SupplierEmail = supplierEmail;
        SupplierPhone = supplierPhone;
    }

    @PropertyName("ItemName")
    public String getItemName() { return ItemName; }
    @PropertyName("Quantity")
    public String getQuantity() { return Quantity; }
    @PropertyName("SerialNo")
    public String getSerialNo() { return SerialNo; }
    @PropertyName("SupplierName")
    public String getSupplierName() { return SupplierName; }
    @PropertyName("SupplierEmail")
    public String getSupplierEmail() { return SupplierEmail; }
    @PropertyName("SupplierPhone")
    public String getSupplierPhone() { return SupplierPhone; }
}

关于java - Firebase Android ListView 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54125503/

相关文章:

java - 还有其他方法可以用 Intent 传递复杂对象吗?

android - GridView onScrollPositionChanged?

android - 从命令提示符安装 apk 文件?

javascript - react JS : How to pass emoji to Firebase real-time db - why is it showing as object?

android - Firebase 中的数据库结构

java.lang.ArrayIndexOutOfBoundsException : length=4; index=4 length array 异常

java - 膨胀类 fragment InflateException 二进制 XML 文件时出错

android - 在 Facebook 安卓上分享分数

ios - Flutter 不会构建 iOS 版本 - 完成

java - 渲染期间引发异常 : TabHost requires a TabWidget with id "android:id/tabs"