Firebase Android ListView 未显示

2024-01-23

我试图在用户登录后在主菜单屏幕的列表视图上显示 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);

    } 

自定义列表适配器.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;
    }
}

内容_主_菜单.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(),这是不正确的,因为 Firebase 正在数据库中查找名为的字段itemName并不是ItemName。看小写的i字母与大写字母I?

有两种方法可以解决此问题。第一个是通过根据以下内容重命名字段来更改模型类Java 命名约定 https://www.javatpoint.com/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 class CustomListAdapter {
    public String itemName, quantity, serialNo, supplierName, supplierEmail, supplierPhone;
}

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

还有第二种方法,就是使用annotations。因此,如果您更喜欢使用私有字段和公共 getter,则应该使用属性名称 https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/PropertyName注释仅在 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; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Firebase Android ListView 未显示 的相关文章

  • Android 中的垂直和水平滚动视图

    我真的很累了寻找垂直和水平滚动视图的解决方案 我读到框架中没有任何视图 布局实现此功能 但我需要这样的东西 我需要在其他布局中定义一个布局 子布局必须实现垂直 水平滚动才能移动 最初实现了一个逐像素移动布局的代码 但我认为这不是正确的方法
  • Android系统每个应用程序的通知限制

    这可能偏离主题 但我找不到任何相关内容 Android应用程序可以显示的通知数量有限制吗 我在收到 100 条通知后遇到问题 没有文件明确说明这一点 注意 显示 100 条通知并不是一个好主意 但由于某些原因这是必需的 In API23 包
  • 不要模拟值对象:过于通用的规则,没有解释

    以下是 Mockito 单元测试框架的引用 不要模拟值对象 为什么有人会想要这样做呢 因为实例化对象太痛苦了 gt 无效 原因 如果创造新的装置太困难 那就是一个迹象 代码可能需要一些认真的重构 另一种方法是创建 价值对象的构建者 有一些工
  • 在 eclipse 之外将 Spring MVC 应用程序部署到 tomcat 的幕后会发生什么?

    我猜想使用像 eclipse 这样很棒的 IDE 的一个缺点是你会忽略应用程序幕后发生的事情 我是一名 Ruby 开发人员 所以不是一名 Java 老手 所以我一直在用 java 编写一个项目 并使用 spring 框架进行 IOC 和 M
  • 从代码动态更改多个文本视图的大小(没有“磁盘上”xml 主题)?

    我有 10 个文本视图在我的代码中 我想更改所有代码的字体大小 在我的布局中我使用了 style定义通用属性 但是我不知道一旦布局出现在屏幕上如何从代码中更改它们 我不想做的是更新 AND 对象 但只写在一处 我知道我可以使用应用主题但这假
  • 在哪里可以获得用于导入 libcore.io 的 JAR?

    我想处理一个GaiException在我的应用程序中 调试器告诉我 它位于 libcore io 包中 但导入它会产生错误 我想我需要向我的项目添加一个额外的 JAR 才能正确解析此类型 我在我的 android sdk 文件夹中进行了一些
  • 带动画的 ScrollTo(0,250) Android ScrollView

    当我滚动到 0 250 时 我想在滚动动作中包含一个动画 我做了这段代码 但它没有根据动画滚动 scrollMe 是滚动小部件 id ObjectAnimator anim ObjectAnimator ofInt scrollMe tra
  • 如何在kotlin中使用Coroutine每秒调用一个函数

    我刚刚创建了一个应用程序 其中我的函数 getdata 每秒调用一次以从服务器获取新数据 而 updateui 函数将更新 UI 中的视图 我在我的应用程序中不使用任何异步任务或协程 我想这样做 请告诉我我怎样才能做到这一点 这是我的代码
  • 在 Android Studio 4.0(Canary) 中找不到预览窗口在哪里

    我正在浏览有关在 Android Studio 4 0 Canary 中运行的 Jetpack 的教程 请参阅下面的链接文章 https developer android com jetpack compose tutorial http
  • Phonegap facebook 插件:android 的各种问题

    我正在尝试将 Phonegap 3 1 与 Phonegap facebook plugin 集成 以使我的应用程序能够使用 facebook 登录 https github com phonegap phonegap facebook p
  • 我们可以有虚假中断吗?

    我正在创建一个任务轮询器 每分钟都会查找任务 它看起来像这样 public class Poller private final ExecutorService e Executors newSingleThreadExecutor pub
  • 停止前台运行的服务的正确方法是什么

    我正在尝试停止作为前台服务运行的服务 当前的问题是当我打电话时stopService 通知仍然保留 因此 在我的解决方案中 我添加了一个接收器 我正在将其注册到onCreate 在 的里面onReceive 我调用的方法stopforegr
  • firebase createUser 无需登录[重复]

    这个问题在这里已经有答案了 我的管理员用户可以创建其他用户 但每次创建用户时 我的管理员用户都会注销 新用户会自动登录 有什么方法可以在不登录的情况下创建用户吗 None
  • 查找配对的 Android 蓝牙设备是否在范围内的正确方法?

    我想编写一个充当蓝牙客户端的应用程序 我想做的是找出最好的方法来确定我支持的特定设备是否在范围内 而不是一直尝试在其上执行 BluetoothDevice connect 并失败如果不在范围内 这里我们假设设备已经配对 恐怕在特定设备处于范
  • 读/写带有特殊字符的.txt文件

    I open Notepad Windows 并写 Some lines with special characters Special 并前往另存为 someFile txt 与Encoding set to UTF 8 在Java中我有
  • Selenium - 等待网络流量

    我们将 Selenium 与 Java API 和一些 Javascript 用户扩展一起使用 我们在应用程序中使用了大量 AJAX 调用 我们的许多测试随机失败 因为有时 AJAX 调用完成得比其他时候慢 因此页面未完全加载 我们通过等待
  • android 媒体播放器 - 如何禁用范围请求? (Nexus 7 上的音频流中断)

    我有一个音频流应用程序 它运行本地代理服务器 本地代理服务器与互联网流媒体源建立 http 连接 在本地获取并缓冲流数据 然后 在应用程序内部 我使用 MediaPlayer 连接到本地代理服务器 使用方法 mediaPlayer setD
  • 如果抛出RuntimeException,是否可以将其作为异常捕获?

    如果我有一个try抛出一个块RuntimException子类 可以是后续的catch块将其捕获为Exception 具体来说 public class MyAppException extends RuntimeException In
  • H2 用户定义的聚合函数 ListAgg 不能在第一个参数上使用 DISTINCT 或 TRIM()

    所以我有一个 DB2 生产数据库 我需要在其中使用可用的函数 ListAgg 我希望使用 H2 的单元测试能够正确测试此功能 不幸的是H2不直接支持ListAgg 但是 我可以创建一个用户定义的聚合函数 import java sql Co
  • 如何检测用户是否禁用 GPS(Android - Play 服务)

    我使用 gms location LocationListener Google Play 服务 来获取用户的位置 它工作正常 但我想检测用户何时禁用或启用他 她的 GPS 就像这张照片一样 当我打开 关闭位置时 不会调用任何方法 当我切换

随机推荐