Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在recyclerview kotlin中显示firebase实时数据库中的数据_Android_Kotlin_Firebase Realtime Database - Fatal编程技术网

Android 在recyclerview kotlin中显示firebase实时数据库中的数据

Android 在recyclerview kotlin中显示firebase实时数据库中的数据,android,kotlin,firebase-realtime-database,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?) {

我想显示来自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

}

}

如果要在回收器视图中显示产品列表,则需要在数据库中有一个产品列表。在屏幕截图中,我们看到您只有
/Products
下单个产品的属性,这不足以填充回收器视图。Firebase将尝试从您的每个单独属性(如
description
image
)加载产品,但无法加载,因为它尝试将单个值转换为整个产品对象


您需要的结构是:

Products: {
  "someProductId": {
    "description: "...",
    "imge: "...",
    "pname: "...",
    "price: "..."
  }
}
现在,如果您有更多产品,您将拥有:

Products: {
  "someProductId": {
    "description: "...",
    "imge: "...",
    "pname: "...",
    "price: "..."
  },
  "anotherProductId": {
    "description: "...",
    "imge: "...",
    "pname: "...",
    "price: "..."
  }
}
您可以将其加载到recycler视图中,并使用已有的代码


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

productsRef.child("someProductId").setValue(product);
如果他们没有ID,您可以让Firebase通过以下方式为您生成一个ID:

productsRef.push().setValue(product);

关于FirebaseRecyclerAdapter有一个很好的教程。自从使用firebase侦听器构建自定义适配器以来,我一直没有用完。我想你也会有同样的结果

另外,您将指派两名布局经理

通常初始化如下所示:

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

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

这段代码中到底有什么不符合您的预期?启动应用程序时看不到产品,但我想尝试只显示一个,然后添加更多我是新加入的,我想测试只显示一个产品,看看它是否有效
//Setup recycler view
    viewManager = LinearLayoutManager(activity)
    viewAdapter = YourAdapter( ... )

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