Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
48 views
in Technique[技术] by (71.8m points)

android - Getting data from firebase in androd studio

How to get data from firebase? I can implement some data on it, but I do now know why I cannot get and put it in my activity, application.

This is my ValuEventListener, should I use here Livedata?

class FirebaseDB : LiveData<List<Shopping>>() {

    var fbItemCount:Long = 0
    private val firebaseDB2: FirebaseDatabase = FirebaseDatabase.getInstance()
    private val userID = FirebaseAuth.getInstance().uid
    public val ref = firebaseDB2.getReference("user/"+userID.toString())

    fun removeAll() {

    }

    fun delete(shopping: Shopping){

    }

    fun modify(shopping: Shopping) {

    }

    fun add(shopping: Shopping) {
        ref.child(shopping.id.toString()).setValue(shopping)
    }

    fun getShopping(): List<Shopping> {
            val lista: ArrayList<Shopping> = ArrayList()

        ref.addValueEventListener(object : ValueEventListener{
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (messageSnapshot in dataSnapshot.children) {
                    val shopping: Shopping = Shopping(id = messageSnapshot.child("id").value as Long,
                        product = messageSnapshot.child("product").value as String,
                        quantity = messageSnapshot.child("quantity").value as String,
                        price = messageSnapshot.child("price").value as String,
                        bought = messageSnapshot.child("bought").value as Boolean)

                        //Log.i("readDB", "$product $quantity $price $isbought")
                    lista.add(shopping)

                }
            }

            override fun onCancelled(error: DatabaseError) {
                Log.e("MyAdapter", "Failed to delete value.",error.toException())
            }

        })

        fbItemCount = lista.size.toLong()

        return lista

    }

}

How can i check if it is downloaded somewhere on my app or I just cannot see it or implement well.

EDIT:

I just added log.v... and it starts working... However after adding a new element in the list, new element is added with double information from firebase for example:

In database I have: Orange Apples

I add: Bananas

Now in application it is like: Orange Apples Orange Apples Bananas

question from:https://stackoverflow.com/questions/65646683/getting-data-from-firebase-in-androd-studio

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

about your double items problem this code making it because before making ValueEventListener you creted an empty list then after get data form firebase in onDataChange method you add all itsm that firebase return then suppose you add new item by calling 'add' mthod them onDataChange wiil we triggred again and giving you again all items with new one added btu you never remove old item and add these new items again it thi list that why is happing

    val lista: ArrayList<Shopping> = ArrayList()

        ref.addValueEventListener(object : ValueEventListener{
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (messageSnapshot in dataSnapshot.children) {
                    val shopping: Shopping = Shopping(id = messageSnapshot.child("id").value as Long,
                        product = messageSnapshot.child("product").value as String,
                        quantity = messageSnapshot.child("quantity").value as String,
                        price = messageSnapshot.child("price").value as String,
                        bought = messageSnapshot.child("bought").value as Boolean)

                        //Log.i("readDB", "$product $quantity $price $isbought")
                    lista.add(shopping)

                }
            }

            override fun onCancelled(error: DatabaseError) {
                Log.e("MyAdapter", "Failed to delete value.",error.toException())
            }

        })

correct code would like this

    var lista = emptyList<Shopping>()

        ref.addValueEventListener(object : ValueEventListener{
            override fun onDataChange(dataSnapshot: DataSnapshot) {

            lista = emptyList()

                for (messageSnapshot in dataSnapshot.children) {
                    val shopping: Shopping = Shopping(id = messageSnapshot.child("id").value as Long,
                        product = messageSnapshot.child("product").value as String,
                        quantity = messageSnapshot.child("quantity").value as String,
                        price = messageSnapshot.child("price").value as String,
                        bought = messageSnapshot.child("bought").value as Boolean)

                        //Log.i("readDB", "$product $quantity $price $isbought")
                    lista.add(shopping)

                }
            }

            override fun onCancelled(error: DatabaseError) {
                Log.e("MyAdapter", "Failed to delete value.",error.toException())
            }

        })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...