首页 > 解决方案 > 如何使用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 而不是!!所以,现在我该怎么办!?

标签: javaandroidkotlin

解决方案


来自我的模板:如何从 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
}

推荐阅读