2015-10-25 37 views
1

我在我的Activity的AsyncTask中初始化ProgressDialog时遇到问题,java文件Activity的名称是BigBoard.java如何在AsyncTask中运行ProgressDialog

这里是的AsyncTask的java类代码里面BigBoard.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); 


} 
+0

'progress = new ProgressDialog(BigBoard.this); '应该工作,请告诉我们_Logcat_出错。 –

+0

其语法错误,用波浪红色加下划线 – OBX

+0

'progress = new ProgressDialog(context);'替换上下文的是什么? – OBX

回答

1

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

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

完成干杯!

+0

谢谢,但什么是'mContext',无法解析符号:( – OBX

+0

请参阅编辑答案 –