在kotlin中隐藏警报对话框
我当前的项目从服务器下载数据时,我想显示一个进度条,以便用户知道发生了什么。我有一个简单的警报对话框,我尝试在为用户加载json数据时添加警报对话框。启动应用程序时,即使数据已加载且未隐藏,警报对话框仍保持在屏幕上 异步任务代码:在kotlin中隐藏警报对话框,kotlin,Kotlin,我当前的项目从服务器下载数据时,我想显示一个进度条,以便用户知道发生了什么。我有一个简单的警报对话框,我尝试在为用户加载json数据时添加警报对话框。启动应用程序时,即使数据已加载且未隐藏,警报对话框仍保持在屏幕上 异步任务代码: inner class Arr : AsyncTask<String, String, String>(){ val progressDialog = AlertDialog.Builder(this@MainActivity)
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
}
} //
内部类Arr:AsyncTask(){
val progressDialog=AlertDialog.Builder(this@MainActivity)
val dialogView=layoutInflater.flate(R.layout.progress_对话框,null)
val message=dialogView.findviewbyd(R.id.message\u id)
val dialog=progressDialog.create()
覆盖乐趣onPreExecute(){
super.onPreExecute()
progressDialog.setMessage(“加载”)
progressDialog.setCancelable(false)
progressDialog.show()
}
//用于构建连接
重写fun doInBackground(varargurl:String?):String{
变量文本:字符串
val connection=URL(URL[0])。openConnection()作为HttpURLConnection
试一试{
connection.connect()
text=connection.inputStream.use{it.reader()。use{reader->reader.readText()}
}最后{
连接断开()
}
返回文本
}
重写onPostExecute(结果:字符串?){
super.onPostExecute(结果)
handleJson(结果)
dialog.discover()的
}
重写进程更新(vararg text:String?){
dialog.discover()的
}
private-fun-handleJson(jsonString:String?){
dialog.discover()的
val jsonObj=JSONObject(jsonString)
val result=jsonObj.getJSONObject(“结果”)
val response=result.getJSONObject(“响应”)
val airport=response.getJSONObject(“机场”)
val pluginData=airport.getJSONObject(“pluginData”)
val schedule=pluginData.getJSONObject(“schedule”)
val arrivals=schedule.getJSONObject(“arrivals”)
//val data=arrivals.getJSONObject(“数据”)
val jsonArray=jsonArray(arrivals.get(“data”).toString())
val list=ArrayList()
变量x=0
而(x
有什么解决办法吗 在您的onPreExecute()
但是在onPostExecute()
中,您调用不同的对话框来关闭
override fun onPostExecute(result: String?) {
...
dialog.dismiss()
}
因此,您需要在
onPostExecute()
中调用progressDialog.disease()
progressDialog.disease()
而不是dialog.disease()
更改为:
override fun onPreExecute() {
super.onPreExecute()
dialog.setMessage("loading")
dialog.setCancelable(false)
dialog.show()
}
并将其隐藏在:
dialog.dismiss();
如果我在
onPostExecute()中使用progressDialog.disease()
我在onPostExecute()中使用progressDialog.disease()
,则必须引用AlertDialog
对象,而不是AlertDialog.Builder
l出现红线错误未解决参考辞退问题,为什么只加载文本而不显示图标?我已经在添加progress\u对话框
activity你说的文本和图标是什么意思?
dialog.dismiss();