gpt4 book ai didi

android - Firebase 数据的更改不会更新

转载 作者:行者123 更新时间:2023-11-29 00:53:05 25 4
gpt4 key购买 nike

我正在试验一款下单和送货应用。我的想法是,在我下订单后,直到确认时,订单才会显示在我的应用程序屏幕上。我可能会提出多个订单查询,直到它们被接受时,它们才会显示在我的屏幕上。数据存储在 Firebase 中,屏幕上的订单列表是动态创建的。

问题是,如果在 Firebase 中更改了订单数据,例如,如果我从 Firebase 中删除数据,则屏幕上不会反射(reflect)相同的数据。只有当我从模拟器中删除应用程序并重新安装它时,屏幕上才会出现正确的内容。这意味着问题不在 Firebase 中,而在于从 Firebase 获取它。

这是我从 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()
}
}

还证明了数据在屏幕上的填充方式的快照。忽略红色标记。这是由其他一些代码处理的

enter image description here

最佳答案

只需将您使用的监听器更改为:

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

关于android - Firebase 数据的更改不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57635327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com