How to add Firebase data to a Recycleview and show on Android screen with Kotlin?

Asked

Viewed 227 times

1

How to bring this fire list to the cel screen?

val store = Shop("Fique Linda", "Temos o melhor!", R.drawable.storefiquelinda)

val dataBase = FirebaseDatabase.getInstance()

val myRef = dataBase.getReference("mensagens")

myRef.child("fiqueLinda").child("1").setValue(store)

myRef.addValueEventListener(object : ValueEventListener {

        override fun onDataChange(dataSnapshot: DataSnapshot?) {

            val value = dataSnapshot!!.value as HashMap<*, *>

            for (messageSnapshot in dataSnapshot.children) {

                val name = messageSnapshot.child("fiqueLinda").value as String?
                val age = messageSnapshot.child("age").value as Long?


                //Desta forma eu estou pegando do firebase ou não?

                list.add( Shop(storeFiqueLinda.name, storeFiqueLinda.subTitle!!, storeFiqueLinda.thumbnail!!))
            }

            //list.add( Shop(myRef.key, storeFiqueLinda.subTitle!!, storeFiqueLinda.thumbnail!!) )

            Log.d("VALUE", "Value is: " + value)
        }

        override fun onCancelled(error: DatabaseError?) {

            Log.w("", "Failed to read value.", error!!.toException())
        }
    })

1 answer

1


I simply left the Shop class (responsible for the pojo) without using the arguments to get the Firebase information with onDataChange().

data class Shop (val name : String, val subTitle: String, val thumbnail : Int) {
    constructor() : this("", "",0 )
}

In the Shopadapter class within the onBindViewHolder method beyond the required Firebase I placed within the onDataChange() method the following code:

val dataBase = FirebaseDatabase.getInstance()
        val getRefUm = dataBase.getReference("lojasUm").push()

        //val getRefDois = dataBase.getReference("lojasDois").push()

        val storeFiqueLinda = Shop("", "", R.drawable.storefiquelinda)

        //val storeLugueRoupa = Shop("", "", R.drawable.storeluguerou)


        getRefUm.setValue(storeFiqueLinda)

        //getRefUm.setValue(storeLugueRoupa)

        getRefUm.addValueEventListener(object : ValueEventListener {

            override fun onDataChange(dataSnapshot: DataSnapshot?) {

                val value = dataSnapshot!!.getValue(Shop::class.java)

                var album: Shop = list.get(position)

                holder.titleTextView?.text = value?.name

                holder.subtitleTextView?.text = value?.subTitle

                holder.thumbImageView?.setImageResource(value?.thumbnail!!)

                holder.thumbImageView?.setOnClickListener {

                    when (position) {

                        0 -> {
                            Log.d("teste -> ${position}", "${position}")
                            Toast.makeText(context, "teste ${position} ", Toast.LENGTH_SHORT).show()
                        }

                        1 -> {
                            Log.d("teste -> ${position}", "${position}")
                            Toast.makeText(context, "teste ${position} ", Toast.LENGTH_SHORT).show()
                        }

                        2 -> {
                            Log.d("teste -> ${position}", "${position}")
                            Toast.makeText(context, "teste ${position} ", Toast.LENGTH_SHORT).show()
                        }

                        3 -> {
                            Log.d("teste -> ${position}", "${position}")
                            Toast.makeText(context, "teste ${position} ", Toast.LENGTH_SHORT).show()
                        }

                        4 -> {
                            Log.d("teste -> ${position}", "${position}")
                            Toast.makeText(context, "teste ${position} ", Toast.LENGTH_SHORT).show()
                        }

                        5 -> {
                            Log.d("teste -> ${position}", "${position}")
                            Toast.makeText(context, "teste ${position} ", Toast.LENGTH_SHORT).show()
                        }

                        6 -> {
                            Log.d("teste -> ${position}", "${position}")
                            Toast.makeText(context, "teste ${position} ", Toast.LENGTH_SHORT).show()
                        }
                    }

                }




                Log.d("VALUE", "Valor is: " + value)
            }

            override fun onCancelled(error: DatabaseError?) {

                Log.w("", "Failed to read value.", error!!.toException())
            }
        })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.