为了账号安全,请及时绑定邮箱和手机立即绑定

Firebase Android ListView未显示

Firebase Android ListView未显示

墨色风雨 2020-02-03 14:18:43
用户登录后,我试图在主菜单屏幕的列表视图上显示Firebase实时数据库中的数据。该应用程序正在运行,但未显示数据。这是我数据库中的数据现在为代码。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);    } CustomListAdapter.javapublic 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;    }
查看完整描述

1 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

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


有两种方法可以解决此问题。第一个方法是根据Java命名约定重命名字段来更改模型类。因此,您的模型类应如下所示:


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 class CustomListAdapter {

    public String itemName, quantity, serialNo, supplierName, supplierEmail, supplierPhone;

}

现在,只需删除当前数据,然后使用正确的名称再次添加即可。仅在测试阶段,此解决方案才有效。


还有第二种方法,即使用annotations。因此,如果您更喜欢使用私有字段和公共getter,则应仅在getter之前使用PropertyName批注。因此,您的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; }

}


查看完整回答
反对 回复 2020-02-03
  • 1 回答
  • 0 关注
  • 723 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信