在RecyclerView Kotlin中显示来自Firebase实时数据库的数据

时间:2020-09-10 11:39:58

标签: android kotlin firebase-realtime-database

我想显示来自Firebase的产品数据,但是我不知道自己在做什么错。

家庭活动

class HomeActivity : AppCompatActivity() {

private var productsRef: DatabaseReference? = null
private var layoutManager: RecyclerView.LayoutManager? = null


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_home)


    productsRef = FirebaseDatabase.getInstance().getReference("Products")



override fun onStart() {
    super.onStart()



    val options = productsRef.let {
        FirebaseRecyclerOptions.Builder<Products>()
            .setQuery(it!!, Products::class.java)
            .build()
    }





    val adapter = object : FirebaseRecyclerAdapter<Products, ProductsViewHolder>(options) {

            override fun onBindViewHolder(holder: ProductsViewHolder, position: Int, model: Products) {



                holder.txtProductName.text = model.getPname()
                holder.txtProductDescription.text = model.getDescription()
                holder.txtProductPrice.text = "Price = " + model.getPrice() + "$"
                Picasso.get().load(model.getImage()).into(holder.imageView)




            }

            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductsViewHolder {

                val view: View = LayoutInflater.from(parent.context).inflate(R.layout.product_items_layout, parent, false)
                return ProductsViewHolder(view)
            }
        }


    adapter.startListening()
    recycler_menu.adapter = adapter

    recycler_menu.layoutManager = LinearLayoutManager(this)
    recycler_menu.setHasFixedSize(true)
    recycler_menu.layoutManager = layoutManager

}

}

ProductsViewHolder

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

var txtProductName: TextView = itemView.product_name
var txtProductDescription: TextView = itemView.product_description
var txtProductPrice: TextView = itemView.product_price
var imageView: ImageView = itemView.product_image

}

enter image description here

2 个答案:

答案 0 :(得分:0)

如果要在回收者视图中显示产品列表,则需要在数据库中具有产品的列表。在屏幕截图中,我们看到您仅在/Products下拥有单个产品的属性,不足以填充回收者视图。 Firebase会尝试从您的每个属性(例如descriptionimage)中加载产品,但这样做会失败,因为它试图将单个值转换为整个产品对象。


您需要的结构是:

Products: {
  "someProductId": {
    "description: "...",
    "imge: "...",
    "pname: "...",
    "price: "..."
  }
}

现在,如果您有更多产品,您将拥有:

Products: {
  "someProductId": {
    "description: "...",
    "imge: "...",
    "pname: "...",
    "price: "..."
  },
  "anotherProductId": {
    "description: "...",
    "imge: "...",
    "pname: "...",
    "price: "..."
  }
}

您可以使用已有的代码将其加载到回收器视图中。


要以这种格式存储产品,您需要使用产品ID向数据库添加额外的级别。如果产品已经具有自然ID,则可以执行以下操作:

productsRef.child("someProductId").setValue(product);

如果他们没有ID,则可以使用以下方法让Firebase为您生成一个:

productsRef.push().setValue(product);

答案 1 :(得分:0)

关于FirebaseRecyclerAdapter here,有一个不错的教程。自从使用Firebase侦听器构建自定义适配器以来,我就用光了。我想你会得到相同的结果。

P.S您要分配两次layoutManager

通常初始化如下:

//Setup recycler view
    viewManager = LinearLayoutManager(activity)
    viewAdapter = YourAdapter( ... )

    recyclerView = view!!.findViewById(R.id.recyclerView)
    recyclerView.apply {
        layoutManager = viewManager
        adapter = viewAdapter
    }