我有一个正在工作的项目,并且使用 Firebase Firestore。我已从 mysql 数据库添加了 125 个项目到 Cloud firestore。我在线搜索了 Firebase presentation获取信息,但由于某种原因它对我没有帮助。我看到了 web、swift、c 和 php,但我看不到 KOTLIN 的代码。但经过 3 天的工作后,我在 logcat 中显示了项目。另一个问题是,我搜索了presentation,stackoverflow没有说明如何在RecyclerView
中显示数据。 。如何向 RecyclerView
显示项目使用 KOTLIN?
我想知道如何使用 Kotlin 做到这一点。
最佳答案
据我从你的问题中了解到,你已经成功地在 logcat 中显示了这些项目,对吧?因此,在这种情况下,您还需要执行两个步骤才能在 RecyclerView
中显示数据。
第一步是创建一个自定义适配器,或者如果您愿意,可以使用 FirestoreRecyclerAdapter
,第二步是为您的项目创建一个支架类。最后只需将适配器设置为您的 RecyclerView
即可。
已添加解决方案:
对于 Java 开发人员, this 是建议您从 Cloud Firestore 数据库检索数据并使用 FirestoreRecyclerAdapter
将其显示在 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 = "")
还有一个包含 RecyclerView
的 .XML
文件,它也如下所示:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>
要显示所有产品名称,请按照以下步骤操作。
现在您需要在 Activity 中找到 RecyclerView
并设置 LinearLayoutManager
,但首先您需要以下导入:
import kotlinx.android.synthetic.main.activity_main.*
然后只需使用以下代码行:
recycler_view.layoutManager = LinearLayoutManager(this)
其中 recycler_view
实际上是 RecyclerView
的 id,如上面的 .XML 文件中所示。
然后,您需要创建 Firestore 数据库的根引用和一个 Query
对象,如下所示:
val rootRef = FirebaseFirestore.getInstance()
val query = rootRef!!.collection("products").orderBy("productName", Query.Direction.ASCENDING)
然后您必须创建一个 FirestoreRecyclerOptions
对象,如下所示:
val options = FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query, ProductModel::class.java).build()
在您的 Activity 类中,创建一个如下所示的 holder
类:
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)
}
}
您的 item_product
.XML 文件应如下所示:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view""/>
然后创建一个声明为全局的adapter
字段:
private var adapter: ProductFirestoreRecyclerAdapter? = null
并在您的 Activity 中实例化它,如下所示:
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,代码更加简单,代码行数也更少,但请记住官方文档永远不会为您提供特定的代码,您必须自己创建。
关于android - 使用 KOTLIN 将 Firebase Firestore 中的数据显示到 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51746587/