https://kotlinlang.org logo
#codereview
Title
# codereview
a

Ayden

06/14/2018, 2:42 PM
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

dmulligan

06/14/2018, 3:33 PM
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

Ayden

06/14/2018, 3:45 PM
Sorry Dave. I don't get it.
a

Andreas Sinz

06/14/2018, 4:39 PM
what is the type of
p0.children
?
d

dmulligan

06/14/2018, 4:50 PM
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))
5 Views