Firebase Android ListView 未显示

Posted

技术标签:

【中文标题】Firebase Android ListView 未显示【英文标题】:Firebase Android ListView not being displayed 【发布时间】:2019-06-01 20:00:35 【问题描述】:

一旦用户登录,我正在尝试在主菜单屏幕的列表视图中显示来自 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.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_
    android:layout_
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main_menu">

    <ListView
        android:id="@+id/listView"
        android:layout_
        android:layout_
        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_
    android:layout_
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <android.support.constraint.ConstraintLayout
        android:layout_
        android:layout_
        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_
            android:layout_
            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_
            android:layout_
            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_
            android:layout_
            android:layout_marginTop="8dp"
            android:text="TextView"
            app:layout_constraintTop_toBottomOf="@+id/serialNo" />

        <TextView
            android:id="@+id/name"
            android:layout_
            android:layout_
            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>

【问题讨论】:

***.com/questions/40891268/… 【参考方案1】:

代码中的问题在于,您在 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 字段和公共getter。还有一个更简单的解决方案,直接在公共字段上设置值,如下所示:

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; 

【讨论】:

以上是关于Firebase Android ListView 未显示的主要内容,如果未能解决你的问题,请参考以下文章

在Android中更新firebase数据库时ListView值重复值

Firebase Android ListView 未显示

Firebase Android ListView 未显示

如何使用 Firebase 查询的结果最终在 Android Studio 中填充 ListView

如何从 ListView 中删除 Firebase Android 中的特定节点

Firebase(Android):我的listView上没有显示图像,一旦我第一次将它上传到存储