Displaying a ProgressDialog while waiting for a joined Thread

后端 未结 4 436
日久生厌
日久生厌 2020-12-21 23:42

In my Activity, I load the content for a list from a DB, and want to display a ProgressDialog while it´s loading.
I got both working on it´s own, but if I load the data

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-22 00:04

    You should use AsyncTask instead actually.

    Here is the link to the library. It is fairly simple:

    1) onPreExecute() = show ProgressDialog

    2) doInBackground() = execute your code

    3) onPostExecute() = dismiss ProgressDialog

    Here's a nice tutorial too.

    In general:

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(this.context);
        dialog.setMessage("Loading...");
        dialog.setCanceledOnTouchOutside(false);
    }
    
    @Override
    protected void onPostExecute(String result) {
        if(dialog.isShowing()) {
            dialog.dismiss();
        }
    }
    

提交回复
热议问题