Would you mind to take a look one my `SwipeRefresh...
# codereview
a
Would you mind to take a look one my
SwipeRefreshLayout
? Am I doing something redundant?
Copy code
/*
    Fetching data from Firebase and
    passing the value to Adapter
    */
val addValueEventListener = firebaseReference.addValueEventListener(object : ValueEventListener {
    override fun onCancelled(p0: DatabaseError) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onDataChange(p0: DataSnapshot) {
        if (p0!!.exists()) {
            val vibeList = mutableListOf<Vibes>()
            // V stands for Vibe
            for (v in p0.children) {
                val vibe = v.getValue(Vibes::class.java)
                vibeList.add(vibe!!)
            }
            recyclerView.adapter = VibeAdapter(vibeList)
        }
    }

})


/*
    Fetch data from Firebase and
    passing the value to Adapter whenever
    swipe refresh triggered
    */
swipeRefreshLayout.setOnRefreshListener {
    val addValueEventListener = firebaseReference.addValueEventListener(object : ValueEventListener {
        override fun onCancelled(p0: DatabaseError) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onDataChange(p0: DataSnapshot) {
            if (p0!!.exists()) {
                val vibeList = mutableListOf<Vibes>()
                // V stands for Vibe
                for (v in p0.children) {
                    val vibe = v.getValue(Vibes::class.java)
                    vibeList.add(vibe!!)
                }
                recyclerView.adapter = VibeAdapter(vibeList)
            }
        }

    })
}
d
What about class VibeEventListener(val view: RecyclerView) : ValueEventListener { override fun onCancelled(p0: DatabaseError) {} override fun onDataChange(p0: DataSnapshot) { if (p0!!.exists()) { view.adapter = VibeAdapter(p0.children.map { it.getValue(Vibes::class.java) }) } } }
a
Sorry Dave. I don't get it.
a
what is the type of
p0.children
?
d
I would assume that children is a list, as you are using it in a for loop. The above class does the same thing as you are asking. You can use it as follows firebaseReference.addValueEventListener(VibeEventListener(recyclerView))