0

我有一个名为BookAdder的类。它扩展了AsyncTask并且应该在应用程序上添加书籍列表。还有一些活动调用它,我想在调用它时在屏幕中心显示一个进度条。

现在,我不知道如何在没有在活动的 XML 文件中定义的情况下显示进度条?有没有办法创建一个进度条然后添加它以显示或不显示?

谢谢

4

2 回答 2

0

你可以这样做:

new DownloadData(yourClass.this).execute();


public class DownloadData extends AsyncTask<Void,Void,Void>
{
  ProgressBar pBar;
  Context context;

    public DownloadData(context con)
    {
            context = con;
             pBar = new ProgressBar(context);
            LinearLayout layout = (LinearLayout) context.findViewById(R.id.ProgressBar);
            layout.addView(pBar);
            pBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackgroud(Void.. params)
    {
        // download data
    }


@Override
protected void onPostExecute(Void result) 
{
    super.onPostExecute(result);
    if (pBar!=null) {
        ((LinearLayout)pBar.getParent()).removeView(pBar);
    }
}
}
于 2013-08-31T13:04:54.743 回答
0

尝试这个

    new asyncTask(Your_context).execute();


    private class asyncTask extends AsyncTask<Void, Void, Boolean> 
    {
        Context context;
        ProgressDialog pd;

        asyncTask(Context context)
        {
            this.context = context; 
            pd = new ProgressDialog(activityContext);
        } 
        protected void onPreExecute() 
        {
            pd.setTitle("Loading..");
            pd.setMessage("Please wait ...");
            pd.setCancelable(false);
            pd.show();
        } 
        protected void onPostExecute(Boolean result)
        {
                   // Update your UI. 
            if(pd.isShowing()) pd.dismiss();
        } 

        @Override
        protected Boolean doInBackground(Void... params) 
        {
               // Get all data from web.
        }

@Override
        protected void onProgressUpdate(String... values) 
        {
            super.onProgressUpdate(values);

            pd.setMessage("Please Wait..." + values[0].toString());
        }

        public class Progress 
        {
            public asyncTask task;

            public Progress(asyncTask task) 
            {
                this.task = task;
            }

            public void publish(String value) 
            {
                task.publishProgress(value);
            }
        }
    }

尝试调用进度更新publishProgress(updating_values);

于 2013-08-31T12:57:57.260 回答