在这里,我试图将数据从Firebase实时数据库获取到Recylerview。但是发生了错误

时间:2019-07-24 11:45:14

标签: firebase-realtime-database kotlin

我正在使用Fire-base实时数据库设置一个recyclerview

This is Main Class:






    recyclerView=findViewById(R.id.recycler_menu)
        recyclerView.setHasFixedSize(true)
        layoutManager=LinearLayoutManager(this)
        recyclerView.layoutManager=layoutManager

 override fun onStart() {
        super.onStart()

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

        val adapter = object : FirebaseRecyclerAdapter<Products, ProductViewHolder>(options) {
            @SuppressLint("SetTextI18n")
            override fun onBindViewHolder(productViewHolder: ProductViewHolder, i: Int, products: Products) {
                productViewHolder.txtProductName.text=products.productName
                productViewHolder.txtProductDescription.text=products.productDescription
                productViewHolder.txtProductPrice.text="Price = " + products.productPrice + "Rs"
                Picasso.get().load(products.productImage!!.toInt()).into(productViewHolder.imageView)
            }
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
                val view=LayoutInflater.from(parent.context).inflate(R.layout.products_items_layout, parent, false)
                return ProductViewHolder(view)
            }
        }
        recyclerView.adapter=adapter
        adapter.startListening()
    }

This is ViewHolder Class:






    class ProductViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
    var txtProductName: TextView=itemView.findViewById(R.id.product_name)
    var txtProductDescription: TextView=itemView.findViewById(R.id.product_description)
    var txtProductPrice: TextView=itemView.findViewById(R.id.product_price)
    var imageView: ImageView=itemView.findViewById(R.id.product_image)
    private lateinit var listener: ItemClickListener

    fun setItemClickListener(listener: ItemClickListener) {
        this.listener=listener
    }

    override fun onClick(v: View) {
        listener.onClick(v, adapterPosition, false)
    }
}

This is model Class here






    class Products {
     var productCategory: String? = null
     var productDescription: String? = null
     var productImage: String? = null
     var productName: String? = null
     var productPrice: String? = null
     var productId: String? = null
     var productTime: String?= null
     var productDate: String? = null

    constructor(){

    }

    constructor(productCategory: String?, productDescription: String?, productImage: String?, productName: String?, productPrice: String?, productId: String?, productTime: String?, productDate: String?) {
        this.productCategory=productCategory
        this.productDescription=productDescription
        this.productImage=productImage
        this.productName=productName
        this.productPrice=productPrice
        this.productId=productId
        this.productTime=productTime
        this.productDate=productDate
    }


}

这是错误 “ 2019-07-24 16:20:33.385 2515-2515 / com.creative.onlineshopping E / AndroidRuntime:FATAL EXCEPTION:main     流程:com.creative.onlineshopping,PID:2515     kotlin.KotlinNullPointerException         在com.creative.onlineshopping.HomeActivity $ onStart $ adapter $ 1.onBindViewHolder(HomeActivity.kt:96)         在com.creative.onlineshopping.HomeActivity $ onStart $ adapter $ 1.onBindViewHolder(HomeActivity.kt:90)“

0 个答案:

没有答案