我可以限制ViewFlipper翻转直到执行AsyncTask吗?

时间:2011-07-12 11:48:26

标签: java android

我的应用程序中有一个ViewFlipper,其中包含3 ViewGroups。每个ViewGroup interaction都依赖于数据库中的数据。我正在使用AsyncTask从数据库中读取并在完成后返回Cursor。在执行AsyncTask之前,我只想在ViewFlipper中显示单个视图,说“正在加载数据,请稍候。”,这有可能吗?

2 个答案:

答案 0 :(得分:2)

onPreExecute()中显示进度对话框,然后在onPostExecute()中将其关闭。像这样的东西,

private class MyAsyncTask extends AsyncTask<Integer, Integer, Integer[]> {
    private ProgressDialog myWait = null;

    // This is on the UI thread itself
    protected void onPreExecute() {
        myWait  = new ProgressDialog(MainActivity.this);
        myWait.setMessage("Loading data, please wait");
        myWait.setCancelable(false);
        myWait.show();
    }

    // Separate worker thread is used here 
    protected Integer[] doInBackground(Integer...params) {
        //do the database loading
        return <your result - goes to onPostExecute>;
    }

    // This is on the UI thread itself
    protected void onPostExecute(Integer[] resultCell) {
        if (myWait  != null) {
            myWait.dismiss();
        }
    }
}

答案 1 :(得分:1)

是的,你可以使用progressDialog。这样做,

progressDiaolg=ProgressDialog.show(Activity.this,"","Loading Images...");
 final Thread t=    new Thread(new Runnable() {
                            public void run() {
                                Log.i("Inside Thread", "Downloading Images...");

                                downloadImages();

                              handler.sendEmptyMessage(0);

                            }

                        });
    t.start();
    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            try {

                progressDiaolg.dismiss();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }

    };

我对Asynctask一无所知。因此,请尝试相应地修改此代码段。