简体   繁体   English

Firebase Android ListView 未显示

[英]Firebase Android ListView not being displayed

I am trying to display my data from firebase real time database on a listview in the main menu screen once the users log in. The app is running but its not displaying the data.用户登录后,我试图在主菜单屏幕的列表视图上显示来自 firebase 实时数据库的数据。该应用程序正在运行,但未显示数据。

This is the data in my database这是我数据库中的数据

在此处输入图片说明

Now for the codes.现在是代码。

MainMenu.java This function is called on the OnCreate(). 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自定义列表适配器.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适配器项.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 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 is a custom layout that i created for every data set to be displayed. 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>

The problem in your code lies in the fact that you have in your CustomListAdapter class a field named ItemName but you are using a getter named getItemName() , which is not correct since Firebase is looking in the database for a field named itemName and not ItemName .你已经在你的代码中的谎言在事实问题CustomListAdapter类一场名为ItemName ,但使用的是一个名为吸气getItemName()因为火力地堡是寻找数据库名为场这是不正确itemName而不是ItemName . See the lowercase i letter vs. capital letter I ?看到小写字母i与大写字母I吗?

There are two ways in which you can solve this problem.有两种方法可以解决这个问题。 The first one would be to change your model class by renaming the fields according to the Java Naming Conventions .第一个是通过根据Java 命名约定重命名字段来更改模型类。 So you model class should look like this:所以你的模型类应该是这样的:

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; }
}

See in this example, there are private fields and public getters.在这个例子中,有private字段和公共 getter。 There is also a simpler solution, to set the value directly on public fields like this:还有一个更简单的解决方案,直接在公共字段上设置值,如下所示:

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

Now just remove the current data and add it again using the correct names.现在只需删除当前数据并使用正确的名称再次添加它。 This solution will work only if you are in testing phase.此解决方案仅在您处于测试阶段时才有效。

There is also the second approach, which is to use annotations .还有第二种方法,即使用annotations So if you prefer to use private fields and public getters, you should use the PropertyName annotation only in front of the getter.因此,如果您更喜欢使用私有字段和公共 getter,则应仅在 getter 前使用PropertyName注释。 So your CustomListAdapter class should look like this:所以你的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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM