Firebase Android ListView未显示 [英] Firebase Android ListView not being displayed

查看:47
本文介绍了Firebase Android ListView未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户登录后,我试图在主菜单屏幕的列表视图上显示Firebase实时数据库中的数据.该应用程序正在运行,但未显示数据.

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.

这是我数据库中的数据

现在输入密码.

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

MainMenu.java This function is called on the 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

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

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

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是我为要显示的每个数据集创建的自定义布局.

content_main_menu_list.xml is a custom layout that i created for every data set to be displayed.

 <?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的字段,但您正在使用名为,这是不正确的,因为Firebase正在数据库中查找名为 itemName 而不是ItemName的字段.看到小写的i字母与大写字母I?

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. See the lowercase i letter vs. capital letter I?

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

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

在此示例中看到,有private字段和公共获取器.还有一个更简单的解决方案,可以直接在公共字段上设置值,如下所示:

See in this example, there are private fields and public getters. 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.

还有第二种方法,即使用annotations.因此,如果您更喜欢使用私有字段和公共获取程序,则应使用

There is also the second approach, which is to use annotations. So if you prefer to use private fields and public getters, you should use the PropertyName annotation only in front of the getter. So your CustomListAdapter class should look like this:

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未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆