如何在kotlin中添加加载对话框
当我的应用程序加载数据json url时,我试图添加加载对话框,因为在午餐应用程序出现数据之前,屏幕显示为空白 这是密码如何在kotlin中添加加载对话框,kotlin,progressdialog,Kotlin,Progressdialog,当我的应用程序加载数据json url时,我试图添加加载对话框,因为在午餐应用程序出现数据之前,屏幕显示为空白 这是密码 inner class Dep : AsyncTask<String, String, String>(){ // for build connection override fun doInBackground(vararg url: String?): String{ var text
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?) {
}
}
我犯了红线错误
分类器“上下文”没有伴生对象,因此必须在此处初始化
我需要添加进度对话框xml吗 您需要像下面这样创建上下文实例:lateinit var context2:Context,然后是context2=container!!。在activity或Fragment方法中添加上下文,只需通过ProgressDialog传递context2就可以在内部类下声明ProgressDialog。然后在onBeforeExecute()下显示对话框并显示它。将其隐藏在onPostExecuteFunction下(在OnPostExecute中显示对话框太迟)progressdialog已被弃用,请改用progressbar您能给我举个例子吗
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();
}