【问题标题】:hide Alert Dialog in kotlin在 kotlin 中隐藏警报对话框
【发布时间】:2018-11-09 13:30:09
【问题描述】:

我当前的项目从服务器时间下载数据我想显示一个进度条,以便用户知道发生了什么。我有一个简单的警报对话框,我尝试在为用户加载数据 json 时添加警报对话框。当我启动应用程序时,即使数据已经加载并且没有隐藏,警报对话框也会保持在屏幕上。

异步任务代码:

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

        val progressDialog = AlertDialog.Builder(this@MainActivity)
        val dialogView = layoutInflater.inflate(R.layout.progress_dialog,null)
        val message = dialogView.findViewById<TextView>(R.id.message_id)
        val dialog = progressDialog.create()


        override fun onPreExecute() {
            super.onPreExecute()

            progressDialog.setMessage("loading")
            progressDialog.setCancelable(false)
            progressDialog.show()


        }

        //        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?) {

            super.onPostExecute(result)
            handleJson(result)

                dialog.dismiss()


        }

        override fun onProgressUpdate(vararg text: String?) {
                dialog.dismiss()

        }
        private fun handleJson (jsonString: String?){
            dialog.dismiss()

            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("arrivals")
//        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("arrival")


                ))


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

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

        }




    } // 

请问有什么解决办法吗?

【问题讨论】:

    标签: kotlin


    【解决方案1】:

    改成这样:

    override fun onPreExecute() {
        super.onPreExecute()
    
        dialog.setMessage("loading")
        dialog.setCancelable(false)
        dialog.show()
    }
    

    并隐藏它:

    dialog.dismiss();
    

    您必须引用 AlertDialog 对象,而不是 AlertDialog.Builder

    【讨论】:

    • 我有疑问,为什么只有文本加载和图标不显示?我已经添加progress_dialog活动
    • 文字和图标是什么意思?
    【解决方案2】:

    在你的onPreExecute()

    override fun onPreExecute() {
        ...
        progressDialog.show()
    }
    

    但在onPostExecute() 中,您调用不同的对话框来关闭

    override fun onPostExecute(result: String?) {
        ...
        dialog.dismiss()
    }
    

    所以你需要在onPostExecute()中调用progressDialog.dismiss()

    【讨论】:

    • 如果我在onPostExecute() 中使用progressDialog.dismiss() 我得到红线错误未解决的引用关闭
    【解决方案3】:

    progressDialog.dismiss() 而不是dialog.dismiss()

    【讨论】:

    • 如果我在onPostExecute() 中使用progressDialog.dismiss() 我得到红线错误未解决的引用关闭
    猜你喜欢
    • 2016-10-22
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多