CEO tech4lifeapps
CEO tech4lifeapps

Reputation: 895

How can I convert this Firebase Recycler Adapter implementation into an Adapter.kt class?

I have written a Kotlin FirebaseRecyclerAdapter that works just fine as part of my MainActivity. However, I would like to have this code in a separate MainAdapter.kt file/class. How can I do this?

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

Upvotes: 1

Views: 511

Answers (1)

Lionel Briand
Lionel Briand

Reputation: 1813

Given your code you could do something like this :

class MainAdapter(lifecycleOwner: LifecycleOwner) : FirebaseRecyclerAdapter<Category, CategoryHolder>(buildOptions(lifecycleOwner)) {

    companion object {
        private fun buildQuery() = FirebaseDatabase.getInstance()
                .reference
                .child("").child("categories")
                .limitToLast(50)

        private fun buildOptions(lifecycleOwner:LifecycleOwner) = FirebaseRecyclerOptions.Builder<Category>()
                .setQuery(buildQuery(), Category::class.java)
                .setLifecycleOwner(lifecycleOwner)
                .build()

    }

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

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

There are many other ways to handle this problem, this is just an encapsulated version of yours.

Upvotes: 1

Related Questions