java - 如何使用android中的函数做一个异步计数器
问题描述
我正在尝试从函数和 volley 中进行计数器,我正在对 Web 服务中的某些数据进行 for 循环,当数据与我拥有的某些数据相同时,我希望计数器加 1,它很简单,但是当函数结束并返回 onCreate 函数时,计数器变为 0,这是我的代码:
class MainActivity : AppCompatActivity() {
var n1 = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d("counter0", n1.toString())
selectType()
Log.d("counter2", n1.toString())
carCount1.text= "$n1 cars "
}
}
fun selectType() {
val url= "http://mydomen/getallcars.php"
val pd= ProgressDialog(this)
pd.setMessage("Please Wait...")
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER)
pd.show()
val rq= Volley.newRequestQueue(this)
val sr= StringRequest(
Request.Method.GET, url,
Response.Listener { response ->
pd.hide()
//converting the string to json array object
val array = JSONArray(response)
list.clear()
//traversing through all the object
for (i in 0 until array.length()) {
//getting item object from json array
val product = array.getJSONObject(i)
Log.d("testing123", product.toString())
//adding the product to item list
if(product.getString("type") == fandvtext.text.toString()) {
n1=+1
Log.d("counter1", n1.toString())
}
list.add(
Cars(
product.getString("type")
)
)
}
},
Response.ErrorListener { error ->
pd.hide()
Toast.makeText(this, error.message.toString(), Toast.LENGTH_LONG).show()
})
Toast.makeText(this, n1.toString(), Toast.LENGTH_LONG).show()
rq.add(sr)
}
}
class Cars(types: String) {
var types:String = types
}
然后当我运行它时,在 Logcat 中显示了这个
D/counter0: 0
D/counter1: 1
D/counter2: 0
counter0 应该是 0 并且是 counter2 必须是 1 而不是!!所以,现在我该怎么办!?
解决方案
来自我的模板:如何从 android 中的任何异步操作中获取数据
这是你可以做的事情
像这样改变你的功能:
fun selectType(callback:(Int) -> Unit){
....
将此代码更改为:
fun selectType(callback:(Int) -> Unit) {
val url= "http://mydomen/getallcars.php"
val pd= ProgressDialog(this)
pd.setMessage("Please Wait...")
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER)
pd.show()
val rq= Volley.newRequestQueue(this)
val sr= StringRequest(
Request.Method.GET, url,
Response.Listener { response ->
pd.hide()
//converting the string to json array object
val array = JSONArray(response)
list.clear()
//traversing through all the object
var n1 = 0
for (i in 0 until array.length()) {
//getting item object from json array
val product = array.getJSONObject(i)
Log.d("testing123", product.toString())
//adding the product to item list
if(product.getString("type") == fandvtext.text.toString()) {
n1=+1
Log.d("counter1", n1.toString())
}
list.add(
Cars(
product.getString("type")
)
)
}
callback.invoke(n1)
},
然后,在 onCreate 中调用它:
selectType(){ result ->
//here, the result is the value of your count
}
推荐阅读
- java - Rest-Assured - 如何使用 POJO 验证响应正文
- python - 如何在python中将2D图像重塑为3D
- c++ - 使用定义的运算符添加对象不像我预期的那样工作
- java - java - 如何将具有唯一值的两个“字符串”合并到java中的新字符串?使用转换为数组或列表
- codeceptjs - 为 CodeceptJs 设置和配置 Faker Js
- angular - 无需 CSS 即可使 Angular Material 响应的框架
- excel - 如何在 Excel VBA 中更改当前用户选择的字体颜色
- python - Django 的 Non Rest API 框架项目的文档
- javascript - 检查通过 v-model 绑定到 false 值的 Vue.js 复选框
- javascript - 单个元素的打字效果