Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中使用函数实现异步计数器_Java_Android_Kotlin - Fatal编程技术网

Java 如何在android中使用函数实现异步计数器

Java 如何在android中使用函数实现异步计数器,java,android,kotlin,Java,Android,Kotlin,我试着从函数和截击两个方面做一个计数器, 我正在对web服务中的某些数据执行for循环,当数据与我拥有的某些数据相同时,我希望计数器添加1,这很容易,但当函数结束并返回onCreate函数时,计数器变为0,下面是我的代码: class MainActivity : AppCompatActivity() { var n1 = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInsta

我试着从函数和截击两个方面做一个计数器, 我正在对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
计数器0应该是0,它是 计数器2必须是1,而不是!! 那么,现在我该怎么办

从我的模板中:

这是你能做的事

如下更改您的函数:

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
}
从我这里的模板:

这是你能做的事

如下更改您的函数:

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
}