gpt4 book ai didi

android - fragment 保留旧信息(未正确更新)

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

我正在使用 Kotlin 开发一个应用程序,当涉及到我的一个名为 ChartsFragment 的特定 fragment 时,我遇到了一些问题。基本上发生的事情是,当单击抽屉导航项目时,我将从数据库中查询数据并将其传递给名为 ChartsFragment 的 fragment 以进行显示。我正在使用 Graph View在 fragment 上绘制我的数据。现在,一切正常,只是出于某种原因,我必须为 fragment 单击导航图标两次才能加载更新的数据。单击一次导航图标不会更新数据。

这是一个全局变量,用于存储来自数据库查询的数据:

private var completed = mutableListOf<String>()

这是我从数据库中查询数据时的代码:

/**
* For firebase database
* Read user progress from db
*/
private fun getProgress() {
val uid = FirebaseAuth.getInstance().getCurrentUser()?.uid

val rootRef = FirebaseDatabase.getInstance().getReference()
val progressRef = rootRef.child("Users").child(uid!!).child("Progress")
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot:DataSnapshot) {
for(dSnapshot : DataSnapshot in dataSnapshot.getChildren()) {
for(ds : DataSnapshot in dSnapshot.getChildren()) {
val key = ds.getKey() as String
completed.add(key)
Log.d(TAG2, key)
}
}
}

override fun onCancelled(@NonNull databaseError : DatabaseError) {
Log.d(TAG2, databaseError.getMessage())
}
}
progressRef.addListenerForSingleValueEvent(valueEventListener)
}

这是我在点击抽屉导航图标 Charts 时的代码:

        R.id.nav_chart -> {
val currentUser = mAuth!!.currentUser
if (currentUser != null) {
getProgress()

var basics = 0
var loops = 0
var functions = 0

completed.forEach {
if (it == "Data Type" || it == "Operation" || it == "Condition" || it == "Null Safety") {
basics++
} else if (it == "For" || it == "Break" || it == "Repeat" || it == "When" || it == "While") {
loops++
} else if (it == "Call" || it == "Main" || it == "Recursion" || it == "Single Expression") {
functions++
}
}
completed.clear()

val bundle = Bundle()
bundle.putDouble("basics",basics.toDouble())
bundle.putDouble("loops",loops.toDouble())
bundle.putDouble("functions",functions.toDouble())
chartFragment(bundle)

} else {
Toast.makeText(applicationContext, R.string.sign_in_to_track, Toast.LENGTH_SHORT).show()
}
}

这是我加载 fragment 时的代码:

/**
* For loading fragments
*/

fun chartFragment(bundle: Bundle){
this.setTitle(R.string.navigation_drawer_chart)
val fragmentManager = supportFragmentManager
val transaction = fragmentManager.beginTransaction()
val fragment = ChartFragment()
fragment.setArguments(bundle)
transaction.replace(R.id.frameLayout1, fragment)
transaction.commit()
}

这是我 fragment 的onCreateView:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {

val view = inflater.inflate(R.layout.fragment_chart, container, false)
val graph = view?.findViewById(R.id.graph) as GraphView
val basics = arguments!!.getDouble("basics")
val loops = arguments!!.getDouble("loops")
val functions = arguments!!.getDouble("functions")
val series = LineGraphSeries(arrayOf(
DataPoint(0.0, 0.0),
DataPoint(1.0, basics),
DataPoint(2.0, loops),
DataPoint(3.0, functions),
DataPoint(4.0, 0.0)
))

series.setColor(Color.GREEN)
series.setDrawDataPoints(true)
series.setAnimated(true)

graph.addSeries(series)

return view
}

我在想这是缓存问题吗?有没有办法强制点击抽屉导航的项目作为临时解决方案?

Update: I'm thinking may AsyncTasks would solve the issue but I have no idea on how to implement AsyncTasks tasks. It seems that the most probably reason is that getProgress() may be too slow and the program is executing the next line instead of waiting for getProgress() to finish.

Update: The size of completed I get after the first click is always the size of the previous click (not the updated data from db), on the second click, i will get the current size (current from the db). So, if my db has 0 items, on the first click, ill get 0 which is correct, then if I complete an activity in the app, the db is then updated with 1 item. So, if I click the chart icon now, I should get size 1 but I'm getting size 0. On the second click, Ill get size 1.

Update: So if I then go and complete another activity in the app, my db should now have 2 items. So, if i click the chart icon again, I should get size 2 but I am not still stuck at the previous size which is 1. On my second click for this, Ill get size 2.

最佳答案

It seems that the most probably reason is that getProgress() may be too slow and the program is executing the next line instead of waiting for getProgress() to finish.

是的,程序正在执行 getProgress() 之后的下一行,然后 completed 被更新。但实际上 getProgress() 已经完成了。它的工作只是触发数据库调用。调用本身是异步执行的(即在另一个线程上)。 onDataChange() 执行后,主线程就可以访问结果。这只会在当前运行的函数(处理图标点击的函数)完成后发生。

由于您的逻辑取决于数据库调用的结果,因此您应该将评估新数据的行移至 onDataChange()

关于android - fragment 保留旧信息(未正确更新),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53143010/

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