【问题标题】:How can I use Firebase UI database with Kotlin如何在 Kotlin 中使用 Firebase UI 数据库
【发布时间】:2017-12-20 16:17:29
【问题描述】:
val adapter = FirebaseRecyclerAdapter<Discount, Holder>(
                Discount::class.java,
                R.layout.fragment_main_day_item,
                Holder::class.java,
                FirebaseDatabase.getInstance().getReference()
        ) {
            override fun populateViewHolder(holder: Holder, dis: Discount, pos: Int){

            }
        }

文档是here

如何使用 Kotlin 处理这个问题

编辑

val mAdapter = object : FirebaseRecyclerAdapter<Chat, ChatHolder>(
            Chat::class.java,
            R.layout.fragment_main_day_item,
            ChatHolder::class.java,
            ref) {
        public override fun populateViewHolder(holder: ChatHolder, chat: Chat, position: Int) {

        }
    }

我将 java 转换为 kotlin 并且可以正常工作。

【问题讨论】:

  • 有什么问题?
  • 上面这段代码不起作用。我认为,这不是一个合适的。

标签: android firebase android-recyclerview kotlin firebaseui


【解决方案1】:

这显示了如何编写查询和一个简单的 Holder 类

var query = FirebaseDatabase.getInstance()
        .reference
        .child("").child("categories")
        .limitToLast(50)

val options = FirebaseRecyclerOptions.Builder<Category>()
        .setQuery(query, Category::class.java)
        .setLifecycleOwner(this)
        .build()

val adapter = object : FirebaseRecyclerAdapter<Category, CategoryHolder>(options) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
        return CategoryHolder(LayoutInflater.from(parent.context)
                .inflate(R.layout.category_row, parent, false))
    }

    protected override fun onBindViewHolder(holder: CategoryHolder, position: Int, model: Category) {
        holder.bind(model)
    }

    override fun onDataChanged() {
        // Called each time there is a new data snapshot. You may want to use this method
        // to hide a loading spinner or check for the "no documents" state and update your UI.
        // ...
    }
}

class CategoryHolder(val customView: View, var category: Category? = null) : RecyclerView.ViewHolder(customView) {

    fun bind(category: Category) {
        with(category) {
            customView.textView_name?.text = category.name
            customView.textView_description?.text = category.description
        }
    }
}

【讨论】:

    【解决方案2】:

    upgrading 到 FirebaseUI 3.0 之后,Firebase 实时数据库可以与 Kotlin 一起使用,如下所示

    val options = FirebaseRecyclerOptions.Builder<Chat>()
                .setQuery(chatQuery,Chat::class.java)
                .setLifecycleOwner(this)
                .build()
    
    val adapter = object : FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatHolder {
                return ChatHolder(LayoutInflater.from(parent.context)
                        .inflate(R.layout.row_chat, parent, false))
            }
    
           protected override fun onBindViewHolder(holder: ChatHolder, position: Int, model: Chat) {
                holder.bind(model)
           }
    
           override fun onDataChanged() {
                // If there are no chat messages, show a view that invites the user to add a message.
                mEmptyListMessage.setVisibility(if (itemCount == 0) View.VISIBLE else View.GONE)
           }
        }
    

    【讨论】:

    • 你在 setQuery 方法中传递的“chatQuery”是什么意思
    • @KishanDonga chatQuery 是一个变量,用于保存对 Firebase RTDB 中数据的引用。
    猜你喜欢
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多