0

我的 ProgressDialog 适用于我在我的 oncreate 方法中使用 show() 的情况。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        pd = ProgressDialog.show(this, "Working", "Retreiving Neaby Restaurants", true, true);  // initializes the progress dialog
        pd.setCanceledOnTouchOutside(false);  // on newer versions of android touching outside of the screen will close the dialog. I decided to make it only cancelable when the back button is pressed.
        setContentView(R.layout.nearby_places_list);

但是,如果我在异步任务中使用它,对话框代码将不起作用,因为它不在 on create() 中。争论显示(this, "Working", "Retriiving Neaby Restaurants", true, true) arnt 允许在 onCreate() 之外:

class MyAsync extends AsyncTask<Void, Integer, Boolean>{  // this task is for populating the listview

        @Override 
        protected void onPreExecute() {
            //ProgressDialog.show(context, "Working", "Retreiving Neaby Restaurants");
            pd = ProgressDialog.show(this, "Working", "Retreiving Neaby Restaurants", true, true);  // initializes the progress dialog
            pd.setCanceledOnTouchOutside(false);  // on newer versions of android touching outside of the screen will close the dialog. I decided to make it only cancelable when the back button is pressed.
        }

这有点令人不安,因为我想在异步任务开始时显示对话框,但在调用该任务之前不久。

所以我的问题是如何在 onPreExecute() 的异步任务中完成这项工作,我不想在用户真正开始使用异步任务之前显示()。

4

1 回答 1

1

采用

pd = ProgressDialog.show(yourActivitName.this, "Working", "Retreiving Neaby Restaurants", true, true);

onPreExecute()

实际上是Context活动的。一个重要的东西,你需要在未来经常使用。

您还可以制作一个Class level variable 上下文活动上下文

然后setContentViewonCreate这样初始化它

activityContext=this

然后你也可以使用

pd = ProgressDialog.show(activityContext, "Working", "Retreiving Neaby Restaurants", true, true);

于 2012-11-27T18:45:34.760 回答