2013-08-31 64 views
0

我有一个类BookAdder。它扩展了AsyncTask它应该在应用程序中添加书籍列表。还有一些活动叫它,我想在屏幕中心显示一个进度条。如何在Android中以编程方式在屏幕上显示进度条?

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

感谢

+1

可以使用ProgressDialog来实现这一要求。 –

+0

我想使用一个显示额外音量的progressBar。 Android网站也避免了ProgressDialog。 –

+0

你可以动态创建进度条 – Piyush

回答

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);

+0

正如我告诉上面的评论,我不想使用ProgressDialog。它是由Android官方网站避免的。 –

+0

您可以更新您的下载进度。看到我更新的答案@omidnazifi – Andrain

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); 
    } 
} 
} 
相关问题