修改之前如何在 AsyncTask 中使用变量?
How to use a variable in an AsyncTask before it is modified?
我想在 AsyncTask 中使用我的 class 的一个参数,但是这个参数在 AsyncTask 之后发生变化。所以当执行 AsyncTask 时,这个变量已经改变,我有参数的新值而不是旧值。
我尝试使用 AsyncTask.execute {} 来创建一个 class 扩展 AsyncTask,但无能为力。
data class Case(var id: Long, var nom: String, var nomCourt: String, var couleur: String, var couleurTexte: String, var prix: Double, var nombre: Int = 0)
class Moteur(): Parcelable {
//I didn't past the code of the Parcelable, because it doesn't matter to this problem.
var cases : List<Case> = listOf(
Case(0, "Bouchon 1", "B1", "#008000", "#FFFFFF", 5.0),
Case(1, "Bouchon 2", "B2", "#800000", "#000000", 1.5),
Case(2, "Bouchon 3", "B3", "#000080", "#FFFFFF", 3.0),
Case(3, "Bouchon 4", "B4", "#808000", "#000000", 7.0),
Case(4, "Bouchon 5 qui est long et cher", "B5", "#008080", "#FFFFFF", 15.0)
)
enregistrerCommande() {
//Of course, the property nombre of the Cases have changed before this function is called, but here is not the problem. In the Log, I have the good values and next the wrong one.
Log.i("REGCOM", "avant : $cases")
AsyncTask.execute {
val bdd = Room.databaseBuilder(context!!, MaBdd::class.java, "historique").build()
val dao = bdd.historiqueDao()
Log.i("REGCOM", "après : ${cases[0]}")
for (i in cases) dao.insertAll(Historique(0L, i!!.nombre, false))
idProchainAchat ++
context!!.getSharedPreferences("all", Context.MODE_PRIVATE).edit().putLong("idProchainAchat", idProchainAchat).apply()
bdd.close()
}
cases.forEach{it.nombre = 0}
prixCommande = 0.0
}
}
它应该在我的 db Historique 中注册属性 nombre 的值,但它总是注册 5 次 0。
感谢您的帮助!
PS抱歉我的英语不好,我说法语。
我刚刚找到了一个解决方案,也许不是最好的,但它确实有效。
fun enregistrerCommande() {
val temp = mutableMapOf<Long, Int>()
cases.forEach { temp[it.id] = it.nombre }
doAsync {
val bdd = Room.databaseBuilder(context!!, MaBdd::class.java, "historique").build()
val dao = bdd.historiqueDao()
for ((clef, valeur) in temp) dao.insertAll(Historique(0L, clef, valeur, false))
idProchainAchat ++
context!!.getSharedPreferences("all", Context.MODE_PRIVATE).edit().putLong("idProchainAchat", idProchainAchat).apply()
bdd.close()
}
cases.forEach{it.nombre = 0}
prixCommande = 0.0
}
解决方法是"save" Map 中的数据(我使用 Map 是因为我只需要两个值,但是只要你使用这种方法,List 就可以了,这是行不通的带指针(我认为当你写 val temp = cases
时你会创建另一个列表,但在对象 Case 上使用相同的指针。我认为这是因为我的先例试验。))。我使用了anko的doAsync函数来减少代码量,但是AsyncTask.execute{}
也可以。
尽管我的英语不好,但我希望有人能够使用我的答案。
我想在 AsyncTask 中使用我的 class 的一个参数,但是这个参数在 AsyncTask 之后发生变化。所以当执行 AsyncTask 时,这个变量已经改变,我有参数的新值而不是旧值。
我尝试使用 AsyncTask.execute {} 来创建一个 class 扩展 AsyncTask,但无能为力。
data class Case(var id: Long, var nom: String, var nomCourt: String, var couleur: String, var couleurTexte: String, var prix: Double, var nombre: Int = 0)
class Moteur(): Parcelable {
//I didn't past the code of the Parcelable, because it doesn't matter to this problem.
var cases : List<Case> = listOf(
Case(0, "Bouchon 1", "B1", "#008000", "#FFFFFF", 5.0),
Case(1, "Bouchon 2", "B2", "#800000", "#000000", 1.5),
Case(2, "Bouchon 3", "B3", "#000080", "#FFFFFF", 3.0),
Case(3, "Bouchon 4", "B4", "#808000", "#000000", 7.0),
Case(4, "Bouchon 5 qui est long et cher", "B5", "#008080", "#FFFFFF", 15.0)
)
enregistrerCommande() {
//Of course, the property nombre of the Cases have changed before this function is called, but here is not the problem. In the Log, I have the good values and next the wrong one.
Log.i("REGCOM", "avant : $cases")
AsyncTask.execute {
val bdd = Room.databaseBuilder(context!!, MaBdd::class.java, "historique").build()
val dao = bdd.historiqueDao()
Log.i("REGCOM", "après : ${cases[0]}")
for (i in cases) dao.insertAll(Historique(0L, i!!.nombre, false))
idProchainAchat ++
context!!.getSharedPreferences("all", Context.MODE_PRIVATE).edit().putLong("idProchainAchat", idProchainAchat).apply()
bdd.close()
}
cases.forEach{it.nombre = 0}
prixCommande = 0.0
}
}
它应该在我的 db Historique 中注册属性 nombre 的值,但它总是注册 5 次 0。
感谢您的帮助!
PS抱歉我的英语不好,我说法语。
我刚刚找到了一个解决方案,也许不是最好的,但它确实有效。
fun enregistrerCommande() {
val temp = mutableMapOf<Long, Int>()
cases.forEach { temp[it.id] = it.nombre }
doAsync {
val bdd = Room.databaseBuilder(context!!, MaBdd::class.java, "historique").build()
val dao = bdd.historiqueDao()
for ((clef, valeur) in temp) dao.insertAll(Historique(0L, clef, valeur, false))
idProchainAchat ++
context!!.getSharedPreferences("all", Context.MODE_PRIVATE).edit().putLong("idProchainAchat", idProchainAchat).apply()
bdd.close()
}
cases.forEach{it.nombre = 0}
prixCommande = 0.0
}
解决方法是"save" Map 中的数据(我使用 Map 是因为我只需要两个值,但是只要你使用这种方法,List 就可以了,这是行不通的带指针(我认为当你写 val temp = cases
时你会创建另一个列表,但在对象 Case 上使用相同的指针。我认为这是因为我的先例试验。))。我使用了anko的doAsync函数来减少代码量,但是AsyncTask.execute{}
也可以。
尽管我的英语不好,但我希望有人能够使用我的答案。