【问题标题】:how add loading dialog in kotlin如何在 kotlin 中添加加载对话框
【发布时间】:2018-11-09 09:22:08
【问题描述】:

我正在尝试在我的应用程序加载数据 json url 时添加加载对话框,因为在午餐应用程序出现数据之前显示空白屏幕

这里是代码

inner class Dep : AsyncTask<String, String, String>(){


        //        for build connection
        override fun doInBackground(vararg url: String?): String{

            var text : String
            val connection = URL(url[0]).openConnection() as HttpURLConnection

            try {
                connection.connect()
                text = connection.inputStream.use { it.reader().use{reader -> reader.readText()} }


            } finally{

                connection.disconnect()

            }
            return text
        }

        override fun onPostExecute(result: String?) {

            val progressDialog = ProgressDialog(Context,this@MainActivity)
            progressDialog.setMessage("loading")
            progressDialog.setCancelable(false)
            progressDialog.show()

            super.onPostExecute(result)
            handleJson(result)

            if (progressDialog != null)
                progressDialog.dismiss();

        }

        private fun handleJson (jsonString: String?){

            val jsonObj = JSONObject(jsonString)
            val result = jsonObj.getJSONObject("result")
            val response = result.getJSONObject("response")
            val airport = response.getJSONObject("airport")
            val pluginData = airport.getJSONObject("pluginData")
            val schedule = pluginData.getJSONObject("schedule")
            val arrivals = schedule.getJSONObject("departures")
//        val data = arrivals.getJSONObject("data")
            val jsonArray = JSONArray(arrivals.get("data").toString())

            val list =  ArrayList<FlightShdu>()
            var x = 0
            while (x < jsonArray.length()){

                val jsonObject = jsonArray.getJSONObject(x)



                list.add(FlightShdu(

                    jsonObject.getJSONObject("flight").getJSONObject("identification").getJSONObject("number").getString("default"),
                    jsonObject.getJSONObject("flight").getJSONObject("airline").getString("name"),
                    jsonObject.getJSONObject("flight").getJSONObject("status").getString("text"),
                    jsonObject.getJSONObject("flight").getJSONObject("airline").getJSONObject("code").getString("icao"),
                    jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("scheduled").getString("departure")


                    ))


                x++
            }
            list.forEach(::println)

            val adapter = ListAdapte(this@MainActivity,list)
            flight_dep_list.adapter = adapter

        }

        //    for get items from json api
        override fun onProgressUpdate(vararg values: String?) {

        }


    }

我在onPostExecute中使用了进度对话框代码

 override fun onPostExecute(result: String?) {

        val progressDialog = ProgressDialog(Context,this@MainActivity)
        progressDialog.setMessage("loading")
        progressDialog.setCancelable(false)
        progressDialog.show()

        super.onPostExecute(result)
        handleJson(result)

        if (progressDialog != null)
            progressDialog.dismiss();

    }

我遇到红线错误

classifier 'Context' dose not have a companion object , and thus must be initialized here

我需要添加进度对话框 xml 吗?

【问题讨论】:

  • 你应该在你的内部类下声明 ProgressDialog。然后在 onBeforeExecute() 下分离 Dialog 并显示它。将其隐藏在 onPostExecuteFunction 下(在执行后显示对话框为时已晚)
  • progressdialog 已弃用,请改用进度条
  • 你能给我举个例子吗1

标签: kotlin progressdialog


【解决方案1】:

您需要像这样 lateinit var context2 : Context 实例,然后在您的活动或 Fragment 方法中使用 context2 = container!!.context 并使用 ProgressDialog 传递 context2

【讨论】:

    猜你喜欢
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多