oTwo
oTwo

Reputation: 285

Change in Firebase data does not get updated

I am experimenting with a order placing and delivery app. The idea is that after I place an order, till the time it is confirmed, the order will show in my app screen. I may place multiple order queries and till the time they are accepted they would show up on my screen. The data is stored in Firebase and the order list on the screen is dynamically created.

The problem is that if the order data is changed in Firebase, for example if I delete the data from Firebase, the same is not getting reflected in the screen. Only if I delete the app from the emulator and reinstall it, then the correct thing is coming up on the screen. That means the problem is not in Firebase but it is in getting it from Firebase.

Here is my code for getting the data from Firebase:

activeordertext.setOnClickListener {
if(isNetworkAvailable()){

    val user = FirebaseAuth.getInstance().currentUser
    val userid = user?.uid

    Ref = FirebaseDatabase.getInstance().reference.child("Orders")

    if(userid != null){

        Ref.addListenerForSingleValueEvent(object: ValueEventListener{
            override fun onCancelled(p0: DatabaseError) {
            }

            override fun onDataChange(p0: DataSnapshot) {
                val activeQuerylayout: LinearLayout = findViewById(R.id.activeQueryeorderlayout)
                activeQuerylayout.removeAllViews()
                for (uniqueKeySnapshot in p0.children) {
                    val querykey = uniqueKeySnapshot.key
                    if(querykey!=null){
                        if((p0.child(querykey).child("Customer ID").getValue(String::class.java)==userid)&&(p0.child(querykey).child("Order Status").getValue(String::class.java)=="Query")){

                            var  param: LinearLayout.LayoutParams = LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT, 1.0F)
                            param.setMargins(64,16,64,0)
                   var  param2: LinearLayout.LayoutParams = LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT, 0.3F)
param2.setMargins(16,0,0,0)



                            var activeQuerysublayout = LinearLayout(this)
                            activeQuerysublayout.layoutParams = param
                            activeQuerysublayout.orientation = LinearLayout.HORIZONTAL
                            activeQuerysublayout.weightSum = 1.0F
                            activeQuerysublayout.setBackgroundResource(R.drawable.queryborder)
                            activeQuerylayout.addView(activeQuerysublayout)

                            var queryTimeView = TextView(this)
                            queryTimeView.layoutParams = param
                            queryTimeView.text = p0.child(querykey).child("Time").getValue(String::class.java)
                            queryTimeView.textSize = 16.0F
                            queryTimeView.setTextColor(Color.parseColor("#3488c3"))
                            queryTimeView.setPadding(32,16,0,16)
                            activeQuerysublayout.addView(queryTimeView)

                            var queryAlertLayout = RelativeLayout(this)
                            queryAlertLayout.layoutParams = param2
                            activeQuerysublayout.addView(queryAlertLayout)

                            var queryAlertSubLayout = RelativeLayout(this)
                            queryAlertSubLayout.layoutParams = param2
                            queryAlertSubLayout.id = View.generateViewId()
                            queryAlertLayout.addView(queryAlertSubLayout)

                            var queryAlert = ImageView(this)
                            queryAlert.layoutParams = param2
                            queryAlert.setImageResource(R.drawable.ic_bell)
                            queryAlert.setPadding(0,16,0,0)
                            queryAlertSubLayout.addView(queryAlert)

                        }
                    }
                }
            }

        })

    }


}else{
    Toast.makeText(this, "Please connect to the internet ", Toast.LENGTH_LONG).show()
}
}

Also proving a snapshot of how the data is populated on the screen. Ignore the red badge. That is taken care by some other code

enter image description here

Upvotes: 2

Views: 2846

Answers (1)

Hasnain Sabir
Hasnain Sabir

Reputation: 200

Just change the listener you use to:

Ref.addValueEventListener(object: ValueEventListener{
        override fun onCancelled(p0: DatabaseError) 

Upvotes: 1

Related Questions