如何在 Kotlin 中使用 Firebase UI 数据库

Bur*_*ban 2 android kotlin firebase android-recyclerview firebaseui

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){

            }
        }
Run Code Online (Sandbox Code Playgroud)

文档在这里

我如何使用 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) {

        }
    }
Run Code Online (Sandbox Code Playgroud)

我将 java 转换为 kotlin 并且它有效。

Nit*_*hin 7

之后升级到3.0 FirebaseUI,火力地堡实时数据库可以使用科特林如下

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


CEO*_*pps 5

这展示了如何编写查询和简单的 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)