当使用多个外部AsynTasks时,Android在ListActivity上显示ProgressDialog

时间:2014-05-01 16:10:06

标签: android android-asynctask android-dialog

所以我对Andorid编程完全不熟悉,当从一个单独的类(我的例子中是GetGames)运行AsyncTask时,似乎无法在ListActivity(我的例子中为ScheduleActiviy)上显示ProgressDialog。我试图使用单独的类来重用代码。当我以前将AsyncTask作为嵌入式类时,它似乎工作。我发布了我认为是所有相关代码的内容。任何帮助都会很棒。谢谢!

ScheduleActivity.java

public class ScheduleActivity extends ListActivity 
{   
     private final String PDIALOG_MSG = "Loading schedule...";

     @Override
     public void onCreate(Bundle savedInstanceState) 
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.schedule);

          ArrayList<HashMap<String, String>> gamesList = null;

          try 
          {
               // Loading information in Background Threads
               gamesList = new GetGames(ScheduleActivity.this, PDIALOG_MSG).execute().get();

GetGames.java

public class GetGames extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> 
{   
     private Context context;
     private ProgressDialog pDialog;
     private String pDialogMsg;

     public GetGames(Context ctx, String dialogMsg)
     {
          context = ctx;
          pDialogMsg = dialogMsg;
     }

     @Override
     public void onPreExecute()
     {
          super.onPreExecute();
          pDialog = new ProgressDialog(context);
          pDialog.setMessage(pDialogMsg);
          pDialog.setIndeterminate(false);
          pDialog.setCancelable(false);
          pDialog.show();
     }

     @Override
     public void onPostExecute(ArrayList<HashMap<String, String>> rtnList)
     {
          pDialog.dismiss();
     }

2 个答案:

答案 0 :(得分:0)

您的ProgressDialog可能应该在Activity级别而不是AsyncTask级别上进行控制。从理论上讲,我不明白你为什么这么做是行不通的,但我可以向你展示一种绝对有效的方法(这就是我的工作),它的组织方式有点不同:

//In AsyncTask

@Override
protected void onPreExecute() {
    showProgressDialog(R.string.importing_pages);
}

@Override
public void onPostExecute(Boolean b) {
    hideProgressDialog();
}

//In Activity

public void showProgressDialog(int msgResId) {
    showProgressDialog(getString(msgResId));
}

public void showProgressDialog(String msg) {
    mProgressDialog = ProgressDialogHelper.buildDialog(this, msg);
    mProgressDialog.show();
}

public void hideProgressDialog() {
    if(mProgressDialog != null)
        mProgressDialog.dismiss();
}

//My progress dialog helper class:

public class ProgressDialogHelper {

    /**
     * Creates a generic progress dialog with the specified message
     *
     * @param activity the activity which hosts the dialog. This must be an activity, not a context.
     * @param msgResId the resId for the message to display
     * @return a progress dialog
     */
    public static ProgressDialog buildDialog(Activity activity, int msgResId) {
        return buildDialog(activity, activity.getApplicationContext().getString(msgResId));
    }

    /**
     * Creates a generic progress dialog with the specified message
     *
     * @param activity the activity which hosts the dialog. This must be an activity, not a context.
     * @param msg the message to display
     * @return a progress dialog
     */
    public static ProgressDialog buildDialog(Activity activity, String msg) {
        ProgressDialog dialog;

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            dialog = new ProgressDialog(new ContextThemeWrapper(activity, android.R.style.Theme_Holo_Dialog));
        else
            dialog = new ProgressDialog(activity);

        dialog.setMessage(msg);
        dialog.setCancelable(false);

        return dialog;
    }

}

如果你不想做,你不必做一个帮助班,这就是我组织它的方式。这里的主要思想是进程对话框应由Activity而不是AsyncTask拥有。

此外,使用的上下文必须是活动的,而不是getApplicationContext()。看起来你有这个部分。

答案 1 :(得分:0)

您可以使用AsyncTasks显示进度对话框。这不是问题。我一直这样做。可能是doInBackground()方法的问题。你有什么?

我通常还将AsyncTasks嵌套在Activity类中,以便它可以在onPostExecute()方法中调用其他Activity类方法。否则,为了使它与您的活动进行通信,您将不得不使用类似处理程序或静态引用的内容。

public class TestActivity extends Activity {

    private AsyncTask<Void, Void, ArrayList<String>> bgLoader;
    private ArrayList<String> listOfStuff;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        listOfStuff = new ArrayList<String>();
        textView = (TextView) findViewById(R.id.textView);
        textView.setText("Your list has " + listOfStuff.size() + " items in it!");
        bgLoader = new MyAsyncTask(this, "Waiting...").execute();

    }

    private void resumeDoingStuff() {
        try {
            listOfStuff = bgLoader.get();
            textView.setText("Your list has " + listOfStuff.size() + " items in it!");
        } catch (InterruptedException e) {

            e.printStackTrace();
        } catch (ExecutionException e) {

            e.printStackTrace();
        }
    }

    public class MyAsyncTask extends AsyncTask<Void, Void, ArrayList<String>> {

        private ProgressDialog progressDialog;
        private String message;
        private Context ctx;

        public MyAsyncTask(Context context, String message) {
            this.ctx = context;
            this.message = message;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(ctx);
            progressDialog.setMessage(message);
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected ArrayList<String> doInBackground(Void... params) {

            ArrayList<String> retList = new ArrayList<String>();
            for (int i = 0; i < 10; i++) {
                try {
                    retList.add("TEST STRING " + i);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return retList;
        }

        @Override
        protected void onPostExecute(ArrayList<String> result) {
            progressDialog.dismiss();
            resumeDoingStuff();
        }

    }

}

With Progress

After Progress Dismissal