Ali Ghassan
Ali Ghassan

Reputation: 1180

hide Alert Dialog in kotlin

My current project downloading data from a server time I want to display a progress bar so the user knows what is going on. I have a simple alert dialog , l am try to add alert dialog when data json loading for user . when l launching app the alert dialog stay stick on screen even if the data loaded already and doesn't hide .

AsyncTask code :

  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

        }




    } // 

any solution please ?

Upvotes: 1

Views: 1846

Answers (3)

forpas
forpas

Reputation: 164154

Change to this:

override fun onPreExecute() {
    super.onPreExecute()

    dialog.setMessage("loading")
    dialog.setCancelable(false)
    dialog.show()
}

and hide it with:

dialog.dismiss();

you must refer to the AlertDialog object and not the AlertDialog.Builder.

Upvotes: 2

Blue Jones
Blue Jones

Reputation: 385

progressDialog.dismiss() instead of dialog.dismiss()

Upvotes: 0

ConstOrVar
ConstOrVar

Reputation: 2085

In your onPreExecute()

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

But in onPostExecute() you call different dialog to dismiss

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

So you need to call progressDialog.dismiss() in onPostExecute().

Upvotes: 0

Related Questions