Android 使用KOTLIN显示从Firebase Firestore到RecyclerView的数据
我有一个我工作的项目,我使用Firebase Firestore。我已经从mysql数据库向CloudFireStore添加了125个项目。我搜索Firebase online以获取信息,但出于某种原因,它对我没有帮助。我看到了web、swift、c和php,但我看不到KOTLIN的代码。但工作3天后,我在logcat中显示了项目。另一个问题是,我搜索了演示文稿,但没有指示如何在Android 使用KOTLIN显示从Firebase Firestore到RecyclerView的数据,android,firebase,kotlin,google-cloud-firestore,Android,Firebase,Kotlin,Google Cloud Firestore,我有一个我工作的项目,我使用Firebase Firestore。我已经从mysql数据库向CloudFireStore添加了125个项目。我搜索Firebase online以获取信息,但出于某种原因,它对我没有帮助。我看到了web、swift、c和php,但我看不到KOTLIN的代码。但工作3天后,我在logcat中显示了项目。另一个问题是,我搜索了演示文稿,但没有指示如何在RecyclerView中显示数据。如何使用KOTLIN将项目显示到RecyclerView 我想知道如何处理Kotl
RecyclerView
中显示数据。如何使用KOTLIN将项目显示到RecyclerView
我想知道如何处理Kotlin。我从您的问题中了解到,您已经成功地在日志中显示了项目,对吗?因此,在本例中,您还需要执行两个步骤才能在
RecyclerView
中显示数据
第一步是创建自定义适配器,或者如果需要,可以使用FirestoreRecyclerAdapter
,第二步是为物品创建holder类。最后,只需将适配器设置为您的RecyclerView
,即可
添加的解决方案:
对于Java delopers,建议使用FirestoreRecyclerAdapter
从云Firestore数据库检索数据并在RecyclerView
中显示数据
对于Kotlin开发人员,我将修改上面示例中的代码。假设您的Firestore数据库结构如下所示:
Firestore-root
|
--- products (collection)
|
--- documentIdOne (document)
| |
| --- productName: "Milk"
|
--- documentIdTwo (document)
| |
| --- productName: "Soy Milk"
|
--- documentIdThree (document)
|
--- productName: "Bacon"
class ProductModel (val productName: String = "")
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>
val rootRef = FirebaseFirestore.getInstance()
val query = rootRef!!.collection("products").orderBy("productName", Query.Direction.ASCENDING)
val options = FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query, ProductModel::class.java).build()
private inner class ProductViewHolder internal constructor(private val view: View) : RecyclerView.ViewHolder(view) {
internal fun setProductName(productName: String) {
val textView = view.findViewById<TextView>(R.id.text_view)
textView.text = productName
}
}
private inner class ProductFirestoreRecyclerAdapter internal constructor(options: FirestoreRecyclerOptions<ProductModel>) : FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
override fun onBindViewHolder(productViewHolder: ProductViewHolder, position: Int, productModel: ProductModel) {
productViewHolder.setProductName(productModel.productName)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_product, parent, false)
return ProductViewHolder(view)
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view""/>
adapter = ProductFirestoreRecyclerAdapter(options)
recycler_view.adapter = adapter
一个模型类,看起来也是这样的:
Firestore-root
|
--- products (collection)
|
--- documentIdOne (document)
| |
| --- productName: "Milk"
|
--- documentIdTwo (document)
| |
| --- productName: "Soy Milk"
|
--- documentIdThree (document)
|
--- productName: "Bacon"
class ProductModel (val productName: String = "")
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>
val rootRef = FirebaseFirestore.getInstance()
val query = rootRef!!.collection("products").orderBy("productName", Query.Direction.ASCENDING)
val options = FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query, ProductModel::class.java).build()
private inner class ProductViewHolder internal constructor(private val view: View) : RecyclerView.ViewHolder(view) {
internal fun setProductName(productName: String) {
val textView = view.findViewById<TextView>(R.id.text_view)
textView.text = productName
}
}
private inner class ProductFirestoreRecyclerAdapter internal constructor(options: FirestoreRecyclerOptions<ProductModel>) : FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
override fun onBindViewHolder(productViewHolder: ProductViewHolder, position: Int, productModel: ProductModel) {
productViewHolder.setProductName(productModel.productName)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_product, parent, false)
return ProductViewHolder(view)
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view""/>
adapter = ProductFirestoreRecyclerAdapter(options)
recycler_view.adapter = adapter
以及一个.XML
文件,其中包含一个RecyclerView
,该文件也如下所示:
Firestore-root
|
--- products (collection)
|
--- documentIdOne (document)
| |
| --- productName: "Milk"
|
--- documentIdTwo (document)
| |
| --- productName: "Soy Milk"
|
--- documentIdThree (document)
|
--- productName: "Bacon"
class ProductModel (val productName: String = "")
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>
val rootRef = FirebaseFirestore.getInstance()
val query = rootRef!!.collection("products").orderBy("productName", Query.Direction.ASCENDING)
val options = FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query, ProductModel::class.java).build()
private inner class ProductViewHolder internal constructor(private val view: View) : RecyclerView.ViewHolder(view) {
internal fun setProductName(productName: String) {
val textView = view.findViewById<TextView>(R.id.text_view)
textView.text = productName
}
}
private inner class ProductFirestoreRecyclerAdapter internal constructor(options: FirestoreRecyclerOptions<ProductModel>) : FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
override fun onBindViewHolder(productViewHolder: ProductViewHolder, position: Int, productModel: ProductModel) {
productViewHolder.setProductName(productModel.productName)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_product, parent, false)
return ProductViewHolder(view)
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view""/>
adapter = ProductFirestoreRecyclerAdapter(options)
recycler_view.adapter = adapter
然后只需使用以下代码行:
recycler_view.layoutManager = LinearLayoutManager(this)
其中,recycler\u视图
实际上是RecyclerView
的id,如上面的.XML文件所示
然后,您需要创建Firestore数据库的根引用和如下所示的Query
对象:
Firestore-root
|
--- products (collection)
|
--- documentIdOne (document)
| |
| --- productName: "Milk"
|
--- documentIdTwo (document)
| |
| --- productName: "Soy Milk"
|
--- documentIdThree (document)
|
--- productName: "Bacon"
class ProductModel (val productName: String = "")
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>
val rootRef = FirebaseFirestore.getInstance()
val query = rootRef!!.collection("products").orderBy("productName", Query.Direction.ASCENDING)
val options = FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query, ProductModel::class.java).build()
private inner class ProductViewHolder internal constructor(private val view: View) : RecyclerView.ViewHolder(view) {
internal fun setProductName(productName: String) {
val textView = view.findViewById<TextView>(R.id.text_view)
textView.text = productName
}
}
private inner class ProductFirestoreRecyclerAdapter internal constructor(options: FirestoreRecyclerOptions<ProductModel>) : FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
override fun onBindViewHolder(productViewHolder: ProductViewHolder, position: Int, productModel: ProductModel) {
productViewHolder.setProductName(productModel.productName)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_product, parent, false)
return ProductViewHolder(view)
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view""/>
adapter = ProductFirestoreRecyclerAdapter(options)
recycler_view.adapter = adapter
然后您必须创建一个FirestoreRecyclerOptions
对象,如下所示:
Firestore-root
|
--- products (collection)
|
--- documentIdOne (document)
| |
| --- productName: "Milk"
|
--- documentIdTwo (document)
| |
| --- productName: "Soy Milk"
|
--- documentIdThree (document)
|
--- productName: "Bacon"
class ProductModel (val productName: String = "")
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>
val rootRef = FirebaseFirestore.getInstance()
val query = rootRef!!.collection("products").orderBy("productName", Query.Direction.ASCENDING)
val options = FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query, ProductModel::class.java).build()
private inner class ProductViewHolder internal constructor(private val view: View) : RecyclerView.ViewHolder(view) {
internal fun setProductName(productName: String) {
val textView = view.findViewById<TextView>(R.id.text_view)
textView.text = productName
}
}
private inner class ProductFirestoreRecyclerAdapter internal constructor(options: FirestoreRecyclerOptions<ProductModel>) : FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
override fun onBindViewHolder(productViewHolder: ProductViewHolder, position: Int, productModel: ProductModel) {
productViewHolder.setProductName(productModel.productName)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_product, parent, false)
return ProductViewHolder(view)
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view""/>
adapter = ProductFirestoreRecyclerAdapter(options)
recycler_view.adapter = adapter
现在我们需要创建一个适配器类,在本例中,该类应如下所示:
Firestore-root
|
--- products (collection)
|
--- documentIdOne (document)
| |
| --- productName: "Milk"
|
--- documentIdTwo (document)
| |
| --- productName: "Soy Milk"
|
--- documentIdThree (document)
|
--- productName: "Bacon"
class ProductModel (val productName: String = "")
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>
val rootRef = FirebaseFirestore.getInstance()
val query = rootRef!!.collection("products").orderBy("productName", Query.Direction.ASCENDING)
val options = FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query, ProductModel::class.java).build()
private inner class ProductViewHolder internal constructor(private val view: View) : RecyclerView.ViewHolder(view) {
internal fun setProductName(productName: String) {
val textView = view.findViewById<TextView>(R.id.text_view)
textView.text = productName
}
}
private inner class ProductFirestoreRecyclerAdapter internal constructor(options: FirestoreRecyclerOptions<ProductModel>) : FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
override fun onBindViewHolder(productViewHolder: ProductViewHolder, position: Int, productModel: ProductModel) {
productViewHolder.setProductName(productModel.productName)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_product, parent, false)
return ProductViewHolder(view)
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view""/>
adapter = ProductFirestoreRecyclerAdapter(options)
recycler_view.adapter = adapter
然后创建一个声明为全局的适配器字段:
private var adapter: ProductFirestoreRecyclerAdapter? = null
并在您的活动中实例化它,如下所示:
Firestore-root
|
--- products (collection)
|
--- documentIdOne (document)
| |
| --- productName: "Milk"
|
--- documentIdTwo (document)
| |
| --- productName: "Soy Milk"
|
--- documentIdThree (document)
|
--- productName: "Bacon"
class ProductModel (val productName: String = "")
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>
val rootRef = FirebaseFirestore.getInstance()
val query = rootRef!!.collection("products").orderBy("productName", Query.Direction.ASCENDING)
val options = FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query, ProductModel::class.java).build()
private inner class ProductViewHolder internal constructor(private val view: View) : RecyclerView.ViewHolder(view) {
internal fun setProductName(productName: String) {
val textView = view.findViewById<TextView>(R.id.text_view)
textView.text = productName
}
}
private inner class ProductFirestoreRecyclerAdapter internal constructor(options: FirestoreRecyclerOptions<ProductModel>) : FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
override fun onBindViewHolder(productViewHolder: ProductViewHolder, position: Int, productModel: ProductModel) {
productViewHolder.setProductName(productModel.productName)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_product, parent, false)
return ProductViewHolder(view)
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view""/>
adapter = ProductFirestoreRecyclerAdapter(options)
recycler_view.adapter = adapter
最后,不要忘记覆盖以下两个函数并开始侦听更改:
override fun onStart() {
super.onStart()
adapter!!.startListening()
}
override fun onStop() {
super.onStop()
if (adapter != null) {
adapter!!.stopListening()
}
}
结果是:
正如您在使用Kotlin时所看到的,代码更简单,代码行数更少,但请记住,官方文档永远不会为您提供特定的代码,您必须创建自己的代码。谢谢,但这是java中的信息,我在Kotlin中搜索。你能帮我处理KOTLIN吗?你可以简单地复制Java代码并粘贴到你的IDE中,然后在KOTLIN代码中进行转换,对吗?我尝试过这样做,但没有成功。请帮助我了解KOTLIN。如果你没有看到你的一些代码,如果你不提供你所得到的行为或错误,我就帮不了你什么忙。但我将尝试为您提供该示例的快速改编。@Dika您只需将产品id作为属性存储在文档中,然后像这样使用:productViewHolder.setProductId(productModel.productId)