繁体   English   中英

如何在AsyncTask中运行ProgressDialog

[英]How to run ProgressDialog in AsyncTask

我在Activity的AsyncTask中初始化ProgressDialog时遇到问题,Java文件Activity的名称为BigBoard.java

这是BigBoard.java内部的AsyncTask的Java类代码:

    class syncX extends AsyncTask<String, String, String>
{
    ProgressDialog progress;

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progress = new ProgressDialog(context); //ERROR
        progress.setMessage("Setting BigBoard ");
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setIndeterminate(true);
        progress.show();
    }

    @Override
    protected String doInBackground(String... params)
    {
        return null;

    }

    @Override
    protected void onPostExecute(String file_url)
    {

    }

}

现在,这是我感到困扰的地方, progress = new ProgressDialog(context); 我尝试将其更改为:

progress = new ProgressDialog(this);
progress = new ProgressDialog(BigBoard);
progress = new ProgressDialog(BigBoard.this);

但是,无奈。 如何解决?

EDIT1

下面根据要求提供了BigBoard类。

public class BigBoard extends ActionBarActivity {

ArrayList<String> countryLocal;
String temp;
ArrayAdapter<String> namesArrayAdapter;
ContactInfo ci;
List<ContactInfo> result;
ProgressDialog progress;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Parse.initialize(this, "app-id", "client-key");
    setContentView(R.layout.activity_big_board);
    ci = new ContactInfo();


    RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);
    String abc="hello";

    syncX runner = new syncX();
    runner.execute();




    result = new ArrayList<ContactInfo>();
    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Credentials");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> credentialList, ParseException e) {
            if (e == null) {
                for(int i=0;i<credentialList.size();i++)
                {
                    ci.name  = credentialList.get(i).getString("Name");
                    ci.surname = credentialList.get(i).getString("SurName");
                    ci.email = credentialList.get(i).getString("email");
                    result.add(ci);


                    Log.d("OUT", "So the Val::------> " + credentialList.get(i).getString("email"));
                    //result.notifyDataSetChanged();

                }
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });

    ContactAdapter ca = new ContactAdapter(result);
    recList.setAdapter(ca);


}

进度对话框需要传递活动的上下文。 正如您想尝试的那样。

progress = new ProgressDialog(context);

现在的问题是如何将上下文传递到进度对话框。 只是该类的调用。 class syncX extends AsyncTask<String, String, String>{}应该将上下文传递到进度对话框。 请参阅下面的修改版本类。

class syncX extends AsyncTask<String, String, String>
{
        ProgressDialog progress;
        Context mContext;

        public syncX(Context context){
          this.mContext= context;
        }

         public Context getContext(){
           return mContext;
         }


        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            progress = new ProgressDialog(getContext());
            progress.setMessage("Setting BigBoard ");
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setIndeterminate(true);
            progress.show();
        }

        @Override
        protected String doInBackground(String... params)
        {
            return null;
        }

        @Override
        protected void onPostExecute(String file_url)
        {

        }    
}

在BigBoard类中,它应该看起来像。

new syncX(this).execute();

干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM