0

我創建了這個AsyncTask內部類,現在我想讓它成爲外層。這裏是代碼:如何將此內部AsyncTask轉換爲外部?

private class DownloadDataFromServer extends AsyncTask<String, Integer, String> { 
     ProgressDialog dialog; 
     boolean connErr = false; 
     boolean soErr = false; 

     @Override 
     protected void onPreExecute() { 
      dialog = new ProgressDialog(HomePage.this); !!!!PROBLEM!!!! 
      dialog.setIndeterminate(false); 
      dialog.setMax(100); 
      dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      dialog.setCancelable(false); 
      dialog.setTitle("Download"); 
      dialog.setMessage("Updating..."); 
      dialog.show(); 

      super.onPreExecute(); 
     } 

     @Override 
     protected String doInBackground(String... urls) { 
      // do something 
     } 

     protected void onProgressUpdate(Integer... progress) { 
      // Update the progress 
      dialog.setProgress(progress[0]); 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      if (connErr || soErr) { 
       String msg = "Bad connection"; 
       AlertDialog.Builder builder; 
       builder = new AlertDialog.Builder(HomePage.this); !!!!PROBLEM!!!! 
       builder.setCancelable(false); 
       builder.setTitle("Connection timeout"); 
       builder.setMessage(msg); 

       builder.setPositiveButton("Retry", new DialogInterface.OnClickListener(){ 
        @Override 
        public void onClick(DialogInterface dialog, int which) 
        { 
         dialog.dismiss(); 
         new DownloadDataFromServer().execute(new String[] { "http://www.example.com" }); 
        } 
       }); 

       builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
         finish(); !!!!PROBLEM!!!! 
        } 
       });   
       AlertDialog dialog = builder.create(); 
       dialog.show(); 
      } else { 
       if (Integer.parseInt(result) <= 0) { 
        Toast.makeText(getBaseContext(), "Error!", Toast.LENGTH_SHORT).show(); !!!!PROBLEM!!!! 
       } else { 
        Toast.makeText(getBaseContext(), "OK!", Toast.LENGTH_SHORT).show(); !!!!PROBLEM!!!! 
       } 
      } 
      dialog.dismiss(); 
     } 
    } 

請注意「!!!!問題!!!!」上述代碼中的字符串:我得到的所有錯誤。我試圖應用一些解決方法,但最終我無法使其工作。此外,我也必須使用

private Database db = new Database(this); 

但我不知道如何代替「this」。請幫助我以正確的方式轉換這個內部類。

+0

想想你的內部課堂裏'這個'是什麼,然後你就會知道如何改變它。 – 2014-10-06 13:10:42

+0

我在上面貼出的代碼的第一行也得到這個錯誤:類爲DownloadDataFromServer的非法修飾符;只允許公開,抽象和最終。怎麼了? – smartmouse 2014-10-06 13:28:31

回答

5

你應該做一個參數constructorDownloadDataFromServer類,並通過語境作爲參數,像

Context mCon; 

public DownloadDataFromServer(Context con){ 
this.mCon=con; 
} 

,並用這個mCon背景下的任何地方你DownloadDataFromServer像這樣

 builder = new AlertDialog.Builder(mCon); 

,並在最後來自Activity的所謂DownloadDataFromServer,如

new DownloadDataFromServer(your_Activity.this).execute(......); 
+0

我做到了,但與Toast一起使用的getBaseContext()方法呢?然後如何處理builder.setPositiveButton下的新的DownloadDataFromServer()。execute調用? – smartmouse 2014-10-06 13:27:02

2

使YOUT DownloadDataFromServer有一個構造函數接受HomeScreen,保存在主屏幕中的一個字段構造函數,然後到處使用,而不是HomeScreen.this這一領域。

因爲這個原因,您可能必須將您的一些私人領域HomeScreen公開(或提供訪問者)。

雖然你不想這麼做,但是有什麼優勢?

+0

它不只是一個「這個」的問題,我還必須替換getBaseContext(),例如... – smartmouse 2014-10-06 13:28:02

+0

您可以使用HomeScreen.this.getbaseContext()。由於您已將HomeScreen保存到某個字段(例如mHomeScreen),因此您可以發出mHomeScreen.getBaseContext() – 2014-10-06 14:34:07

+0

它僅適用於HomeScreen。 – smartmouse 2014-10-06 14:42:00

相關問題