使用 KOTLIN 将数据从 Firebase Firestore 显示到 RecyclerView

Div*_*esh 3 android kotlin firebase google-cloud-firestore

我有一个我工作的项目,我使用 Firebase Firestore。我已将 mysql 数据库中的 125 个项目添加到 Cloud Firestore。我搜索了 Firebase 在线演示以获取信息,但由于某种原因它对我没有帮助。我看到了 web、swift、c 和 php,但我看不到 KOTLIN 的代码。但是在工作 3 天后,我在 logcat 中显示了项目。另一个问题是,我搜索了演示文稿,stackoverflow 没有指示如何在RecyclerView. 如何RecyclerView使用 KOTLIN显示项目?

我想知道如何用 Kotlin 做到这一点。

Ale*_*amo 12

正如我从您的问题中了解到的,您已经成功地显示了 logcat 中的项目,对吗?因此,在这种情况下,您还需要执行两个步骤才能在RecyclerView.

第一步是创建一个自定义适配器,或者如果您愿意可以使用FirestoreRecyclerAdapter,第二步是为您的项目创建一个支架类。最后只需将适配器设置为您RecyclerView的即可。

解决方案补充:

对于 Java delopers,是一种推荐的方式,您可以通过这种方式从 Cloud Firestore 数据库中检索数据并将其显示在RecyclerViewusing 中FirestoreRecyclerAdapter

对于 Kotlin 开发人员,我将改编上面示例中的代码。假设您有一个如下所示的 Firestore 数据库结构:

Firestore-root
    |
    --- products (collection)
           |
           --- documentIdOne (document)
           |        |
           |        --- productName: "Milk"
           |
           --- documentIdTwo (document)
           |        |
           |        --- productName: "Soy Milk"
           |
           --- documentIdThree (document)
                    |
                    --- productName: "Bacon"
Run Code Online (Sandbox Code Playgroud)

一个看起来也像这样的模型类:

class ProductModel (val productName: String = "")
Run Code Online (Sandbox Code Playgroud)

一个.XML包含 a的文件RecyclerView也如下所示:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view"/>
Run Code Online (Sandbox Code Playgroud)

要显示所有产品名称,请按照以下步骤操作。

现在您需要RecyclerView在您的活动中找到并设置 ,LinearLayoutManager但首先您需要以下导入:

import kotlinx.android.synthetic.main.activity_main.*
Run Code Online (Sandbox Code Playgroud)

然后只需使用以下代码行:

recycler_view.layoutManager = LinearLayoutManager(this)
Run Code Online (Sandbox Code Playgroud)

其中recycler_view实际上是RecyclerView上面 .XML 文件中所见的 id 。

然后你需要创建 Firestore 数据库的根引用和一个Query像这样的对象:

val rootRef = FirebaseFirestore.getInstance()
val query = rootRef!!.collection("products").orderBy("productName", Query.Direction.ASCENDING)
Run Code Online (Sandbox Code Playgroud)

然后你必须创建一个FirestoreRecyclerOptions这样的对象:

val options = FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query, ProductModel::class.java).build()
Run Code Online (Sandbox Code Playgroud)

在您的活动类中,创建一个如下所示的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
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我们需要创建一个适配器类,在这种情况下应该是这样的:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

您的item_product.XML 文件应该是这样的:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_view""/>
Run Code Online (Sandbox Code Playgroud)

然后创建一个adapter声明为全局的字段:

private var adapter: ProductFirestoreRecyclerAdapter? = null
Run Code Online (Sandbox Code Playgroud)

并在您的活动中实例化它,如下所示:

adapter = ProductFirestoreRecyclerAdapter(options)
recycler_view.adapter = adapter
Run Code Online (Sandbox Code Playgroud)

最后,不要忘记覆盖以下两个函数并开始监听更改:

override fun onStart() {
    super.onStart()
    adapter!!.startListening()
}

override fun onStop() {
    super.onStop()

    if (adapter != null) {
        adapter!!.stopListening()
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

在此处输入图片说明

正如您所看到的,使用 Kotlin,代码更简单,代码行更少,但请记住,官方文档永远不会为您提供特定代码,您必须自己创建。