首页 > 解决方案 > 我从我的火力库中获得了多少数据,但没有输出

问题描述

我的代码没有错误。我得到了存储在其中的数据的大小,但 RecyclerView 中没有 productName、productCode 等输出。我不知道,我已经尽力了,但仍然没有进展。我只是想显示将在卡片视图中设置的数据。所以这就是问题所在。FirebaseDatabase如您所见,我的产品中有 5 个数据。然后我想显示它的详细信息,例如代码、过期日期和名称。在这种情况下,我只测试输出代码和名称。当我运行它时,输出就像这个VirtualAndroidDevice一样,你可以看到我得到了 5 的数据,但应该有一个 2 textView 包含代码和名称。

产品适配器.kt

class ProductAdapter(options: FirebaseRecyclerOptions<Product>) : FirebaseRecyclerAdapter<Product, ProductAdapter.ViewHolder>(options) {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val productCode:TextView = itemView.findViewById(R.id.productCodeTextView)
        val productName:TextView = itemView.findViewById(R.id.NUPTextView)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layout:View = LayoutInflater.from(parent.context).inflate(R.layout.item_product, parent, false)

        return ViewHolder(layout)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int, model: Product) {
        holder.productName.setText(model.getProductName())
        holder.productCode.setText(model.getProductCode())
    }
}

HomeFragment.kt

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val layout = inflater.inflate(R.layout.fragment_home, container, false)
        recyclerView = layout.findViewById(R.id.homeFragmentRecyclerView)
        recyclerView.layoutManager = LinearLayoutManager(context)

        val query: Query = FirebaseDatabase.getInstance().getReference()

        val options: FirebaseRecyclerOptions<Product> = FirebaseRecyclerOptions.Builder<Product>()
            .setQuery(query, Product::class.java)
            .build()

        adapter = ProductAdapter(options)
        recyclerView.adapter = adapter
        return layout
    }

产品.kt

private var code: String? = null
    private var name: String? = null

    fun Product(code: String?, name: String?) {
        this.code = code
        this.name = name
    }

    fun getProductCode(): String? {
        return code
    }

    fun setProductCode(code: String?) {
        this.code = code
    }

    fun getProductName(): String? {
        return name
    }

    fun setProductName(name: String?) {
        this.name = name
    }

对于任何要求 item_product.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:cardBackgroundColor="#FFFFE8">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp">

        <TextView
            android:id="@+id/productCodeTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Product Code"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"/>

        <TextView
            android:id="@+id/NUPTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="NUP"
            android:layout_below="@id/productCodeTextView"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

标签: kotlinandroid-fragmentsfirebase-realtime-databaseandroid-recyclerviewandroid-adapter

解决方案


推荐阅读