简体   繁体   English

完成工作后如何显示 ProgressDialog 并关闭它

[英]How to display a ProgressDialog and dismiss it when finished doing a job

I have a method which collects a large amount of data which slows down my application (all call logs).我有一种方法可以收集大量数据,这会减慢我的应用程序(所有调用日志)的速度。 What I am trying to achieve is that before this method is called, it shows a ProgressDialog , this I have achieved.我想要实现的是,在调用此方法之前,它会显示一个ProgressDialog ,这是我已经实现的。 What I am not capable of is that it makes it dismiss when the method gets all the data and displays it on the screen.我无法做到的是,当该方法获取所有数据并将其显示在屏幕上时,它会关闭它。

For this method I am using the Anko library to make the call to the asynchronous method.对于这个方法,我使用Anko库来调用异步方法。

This is my code:这是我的代码:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = MyActivityBinding.inflate(layoutInflater)
    setContentView(binding.root)

    var pd = ProgressDialog(this)
    pd.show()
    pd.setContentView(R.layout.myCustomLoading)
    pd.window?.setBackgroundDrawableResource(R.color.transparent)
    pd.setCancelable(false)

    doAsync { //Anko library

        val myList = getData()  //This methood return a List<Model>
        binding.list.adapter = MyAdapter(this@MyActivity, myList)

    }

    pd.dismiss()
}

The problems I have with this code is that the ProgressDialog is not showing and to display the data on the screen in the RecyclerView I must touch the screen or drag it, this is rare.这段代码的问题是 ProgressDialog 没有显示,并且要在 RecyclerView 的屏幕上显示数据,我必须触摸屏幕或拖动它,这很少见。

Move pd.dismiss() to the bottom of async block.pd.dismiss()移动到异步块的底部。

But don't forget to run it on UI thread activity.runOnUiThread { pd.dismiss() }但不要忘记在 UI 线程activity.runOnUiThread { pd.dismiss() }上运行它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM