在后台异步任务中加载数据时需要帮助在UI中显示POPup对话框 [英] Need help in showing the POPup dialog in the UI while data loads in background async task

查看:103
本文介绍了在后台异步任务中加载数据时需要帮助在UI中显示POPup对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在异步任务的onPreExecute方法中有一个进度对话框,但是当异步任务从数据库中获取数据时,它不会立即显示.您能否提出一些建议,在单击按钮后立即显示进度对话框.在当前情况下,屏幕在获取数据时冻结.提取后,它将移至下一个屏幕,但与此同时,它不会显示我的进度对话框. 下面是异步任务

I have a progress dialog in onPreExecute method of an async task but it doesn't show immediately while the async task fetches data from the DB. Can you suggest something that will display a progress dialog immediately on button click.. In the current scenario the screen freezes while its fetching data. Once fetched then it moves on to the next screen, but it doesn't show my progress dialog in the meanwhile. Below is the async task

public class waitAsync extends AsyncTask<String, Void, String> {
    ResultSet rs=null;
   private ProgressDialog processingDialog;
    static Connection conn=null;
    Statement stmt;
   // waitThread wt= new waitThread();
    @Override
    protected void onPreExecute() {
      //  ProgressDialog processingDialog;
        super.onPreExecute();
        processingDialog = new ProgressDialog(MainActivity.mCon);
        processingDialog.show();
        processingDialog.setContentView(R.layout.activity_wait);
        //processingDialog.setMessage("Loading...");

    }
    @Override
   protected String doInBackground(String... params) {
        try {


            String driver = "net.sourceforge.jtds.jdbc.Driver";
            String ip = "mfb.dfsdfsdf.ap-north-1.rds.amazonaws.com";
            String classs = "net.sourceforge.jtds.jdbc.Driver";
            String db = "MyDatabase";
            String un = "xxxwww";
            String password = "xxxxxx";
            Class.forName(classs);

            String ConnURL = null;
            ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                    + "databaseName=" + db + ";user=" + un + ";password="
                    + password + ";";
            conn = DriverManager.getConnection(ConnURL);

            stmt = conn.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            rs=stmt.executeQuery(params[0]);
            int size = 0;


            //conn.close();
            MainActivity.getdata=rs;
            MainActivity.flag=1;
            //Thread.sleep(5000);
        } catch (Exception e)
        {
            Log.w("Error connection", "" + e.getMessage());
        }
        return null;
        }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        processingDialog.dismiss();
      //  wt.interrupted();


    }



}

Main_Activity中异步任务的调用

The call of async task from Main_Activity

Getdata(String bookid)
{
  String SQL="Select eb.book_name,eb.AuthorID,ma.Name,eb.GenreID_M,eb.GenreID_D,gm.Genre_MN+'- '+gd.Genre_DN as Genre,isnull(eb.pubDate,'') pubDate,isnull(eb.book_Loc,'') book_Loc,\n" +
                    " eb.Price,eb.ico_Loc,eb.Descrip from Master_ebook eb inner join Master_Author ma on eb.AuthorID=ma.Author_ID\n" +
                    " inner join Master_Genre_M gm on eb.GenreID_M=gm.Genre_MID\n" +
                    "inner join Master_Genre_D gd on eb.GenreID_D= gd.Genre_DID where eb.bookID=" + bookid;


            new waitAsync().execute(SQL);

            int c=0;
            while(MainActivity.flag==0)
            {
                c++;
            }

            if(MainActivity.flag==1)
            {
                MainActivity.flag=0;
                //processingDialog.dismiss();
            }

            ResultSet rs=MainActivity.getdata;
}

推荐答案

在异步任务之后,您不需要像这样的循环.它将阻止您的UI线程.

You don't need a loop like this after async task. It will block your UI thread.

while(MainActivity.flag==0) // wrong - blocking main thread
            {
                c++;
            }

您需要在活动中创建一个方法,例如onDataAvailable,并在此方法中 您可以做任何您想做的事情,要么转到下一个活动,要么什么,然后从onPostExecute

You need to create a method in activity, say onDataAvailable and in this method you can do whatever you want to do, either go to next activity or whatever, then call this method from onPostExecute

  @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        processingDialog.dismiss();
      //  call load complete method here


    }

这是您更新的异步任务.

Here is your updated async task.

public class WaitAsyncTask extends AsyncTask<String, Void, String> {

    private WeakReference<MainActivity> activityRef;

    public WaitAsyncTask(MainActivity activity){
        activityRef = new WeakReference<MainActivity>(activity);
    }

     @Override
     protected void onPreExecute() {
        super.onPreExecute();
        // show dialog
    }
    @Override
    protected String doInBackground(String... params) {
        // do you work in background
        return null;
    }

    @Override
    protected void onPostExecute(String data) {
        super.onPostExecute(data);
        // hide dialog
        if(activityRef.get() != null){
            // call load completed method in activity
            activityRef.get().loadCompleted(data);
        }
    }
} // WaitAsyncTask

然后在您的MainActivity中,创建一个您要在其中接收结果的方法.我已经创建了这种方法

And in your MainActivity, create a method in which you want to receive results. I have create this method

  public void loadCompleted(String data){
        // this will be called automatically after fetching data is completed
    }

我从任务中调用的MainActivity中的

.您不需要while循环来等待.提取数据后,将调用此方法.

in MainActivity which I am calling from my task. You don't need while loop to wait. This method will be called after your data is fetched.

这篇关于在后台异步任务中加载数据时需要帮助在UI中显示POPup对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆