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

1

How to bring this list from fire to the screen of cel?

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())
        }
    })
    
asked by anonymous 24.10.2017 / 12:30

1 answer

1

I simply left the Shop class 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 inside the onBindViewHolder method, in addition to the Firebase requirement, I put the following code inside the onDataChange () method:

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())
            }
        })
    
27.10.2017 / 11:21