使用KOTLIN将数据从Firebase Firestore显示到RecyclerView [英] Display data from Firebase Firestore to RecyclerView with KOTLIN

查看:82
本文介绍了使用KOTLIN将数据从Firebase Firestore显示到RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a project that I work and I use Firebase Firestore. I have added from mysql database 125 items to Cloud firestore. I searched Firebase online presentation to get informations but for some reason it is not helping me. I see web, swift, c and also php but i can not see code for KOTLIN. But after 3 day of work I displayed items in logcat. Another problem is, I searched presentation, stackoverflow there is no indication how to diplay data in RecyclerView. How can I display items to RecyclerView with KOTLIN?

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

I want to know how to do this with Kotlin.

推荐答案

据我所知,您已经成功在logcat中显示了项目,对吗?因此,在这种情况下,您还需要执行两个步骤才能在RecyclerView中显示数据.

As I understand from your question, you have successfully displayed the items in your logcat, right? So in this case there are two more steps that you need to do in order to display the data in a RecyclerView.

第一步是创建一个自定义适配器,或者如果您想使用FirestoreRecyclerAdapter,第二步是为您的商品创建一个holder类.最后,只需将适配器设置为您的RecyclerView即可.

The first step would be to create a custom adapter or if you want you can use FirestoreRecyclerAdapter and the second one is to create a holder class for your item. In the end just set the adapter to your RecyclerView and that's it.

已添加解决方案:

对于Java delopers,

For Java delopers, this is a recommended way in which you can retrieve data from a Cloud Firestore database and display it in a RecyclerView using FirestoreRecyclerAdapter.

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

For Kotlin developers, I'll adapt the code from the example above. Assuming you have a Firestore database structure that looks like this:

Firestore-root
    |
    --- products (collection)
           |
           --- documentIdOne (document)
           |        |
           |        --- productName: "Milk"
           |
           --- documentIdTwo (document)
           |        |
           |        --- productName: "Soy Milk"
           |
           --- documentIdThree (document)
                    |
                    --- productName: "Bacon"

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

A model class that looks also like this:

class ProductModel (val productName: String = "")

和一个包含RecyclerView.XML文件,该文件也看起来像这样:

And a .XML file that contains a RecyclerView which also looks like this:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view"/>

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

To display all the product names, please follow the next steps.

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

Now you need to find the RecyclerView in your activity and set the LinearLayoutManager but first you need the following import:

import kotlinx.android.synthetic.main.activity_main.*

然后使用下面的代码行:

And then just use the following line of code:

recycler_view.layoutManager = LinearLayoutManager(this)

其中的recycler_view实际上是RecyclerView的ID,如上面的.XML文件所示.

In which recycler_view is actually the id of the RecyclerView as seen in the .XML file above.

然后,您需要创建Firestore数据库的根引用和如下的Query对象:

Then you need to create the root reference of your Firestore database and a Query object like this:

val rootRef = FirebaseFirestore.getInstance()
val query = rootRef!!.collection("products").orderBy("productName", Query.Direction.ASCENDING)

然后,您必须创建一个FirestoreRecyclerOptions对象,如下所示:

Then you'll have to create a FirestoreRecyclerOptions object like this:

val options = FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query, ProductModel::class.java).build()

在您的活动类中,创建一个如下所示的holder类:

In your activity class, create a holder class that looks like this:

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
    }
}

现在我们需要创建一个适配器类,在这种情况下应如下所示:

Now we need to create an adapter class which in this case should look like this:

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文件应如下所示:

Your item_product .XML file should like this:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_view""/>

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

Then create an adapter field which is declared as global:

private var adapter: ProductFirestoreRecyclerAdapter? = null

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

And instantiate it in your activity like this:

adapter = ProductFirestoreRecyclerAdapter(options)
recycler_view.adapter = adapter

最后,不要忘记重写以下两个功能并开始侦听更改:

In the end, don't forget to override the following two functions and start listening for changes:

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

override fun onStop() {
    super.onStop()

    if (adapter != null) {
        adapter!!.stopListening()
    }
}

结果是这样的:

正如您所见,使用Kotlin时,代码甚至更简单,代码行也更少,但是请记住,官方文档永远不会为您提供特定的代码,因此您必须创建自己的代码.

As you can see using Kotlin, the code is even simpler and are less lines of code but remember official documentations will never provide you particular code, you'll have to create your self.

这篇关于使用KOTLIN将数据从Firebase Firestore显示到RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆