2015-10-03 120 views
1

我在顯示進度對話框微調器時遇到了問題。我看了幾個主題,提出的解決方案似乎並不奏效。未顯示異步任務進度對話框

AsyncTask中的所有代碼確實被成功調用,但是我看不到正在顯示的進度微調器。這個AsycTask是在一個單獨的類中定義的,它從我的片段中被調用,我通過getActivity()作爲上下文。

我的想法是,這個問題與上下文被賦予進度對話框的方式有關。

public class ExampleClass 
{ 
    private class loginAsyncTest extends AsyncTask<HttpPackage, Void, String> 
    { 
     ProgressDialog dialog; 
     Context spinnerContext; 

     public void setSpinnerContext(Context context) 
     { 
      spinnerContext = context; 
     } 

     @Override 
     protected void onPreExecute() 
     { 
      dialog = new ProgressDialog(spinnerContext); 
      dialog.setMessage("Loading..."); 
      dialog.setIndeterminate(false); 
      dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
      dialog.setCancelable(true); 
      dialog.show(); 
      Log.d("http", "dialog spinner started"); 
     } 

     protected String doInBackground(HttpPackage... httpParameters) 
     { 
      HttpClient client = new DefaultHttpClient(); 
      String url = httpParameters[0].getUrl(); 
      // Get HTTP parameters that are passed in 
      ArrayList<NameValuePair> parameterList = httpParameters[0].getParameters(); 
      HttpPost post = new HttpPost(url); 
      String responseJson = ""; 

      try 
      { 
       post.setEntity(new UrlEncodedFormEntity(parameterList)); 
       HttpResponse response = client.execute(post); 

       BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
       String line = ""; 
       while ((line = rd.readLine()) != null) 
       { 
        responseJson = responseJson + line; 
       } 

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

      return responseJson; 
     } 

     protected void onPostExecute(String result) 
     { 
      if(dialog.isShowing()) 
      { 
       dialog.dismiss(); 
      } 
     } 
    } 
} 

這是對來自提供上下文的片段的ExampleClass的調用。

Public class SampelFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     final View rootView = inflater.inflate(R.layout.dialog_login, container, false);  

     ExampleClass.loginRequest(params, getActivity())); 

     return rootView; 
    } 
} 

例如ExampleClass然後傳遞上下文到我們AsyncTask

public String loginRequest(HttpPackage postParameters, Context context)  
{ 
    loginAsyncTest loginAsync = new loginAsyncTest(context); 
    loginAsync.execute(postParameters); 

    return someString; 
} 
+0

你在哪裏執行? –

+0

@Lakhan我打算從片段中調用異步任務,我希望顯示進度對話框,但異步任務駐留在可以重用的獨立類中。 – mil06

回答

0

嘗試使用ExampleClass.this作爲給定的背景下,確保你調用方法setSpinnerContext()。

此外,我認爲這將是更好的,而不是方法setSpinnerContext()你添加一個構造函數爲loginAsyncTask接收上下文。所以我會用這種替代方法setSpinnerContext(Context context)

public loginAsyncTask(Context context){ 
    this.context = context; 
} 

,你應該實例作爲 new loginAsyncTask(ExampleClass.this).execute();

+0

然而,謝謝你,ExampleClass是一個基本的工具類,從我的理解來說它沒有上下文。 – mil06

0

我沒有看到你曾經被稱爲publishProgress()或覆蓋onProgressUpdate()。在我的應用程序中,我在我的doInBackground()方法中調用publishProgress(),這會更新我的進度條。 Android文檔中的example非常簡單,但非常有用:

您也可以考慮在AsyncTask之外創建進度條,並將其作爲參數傳遞給AsynchTask作爲構造函數或通過setter方法。這樣你就不必懷疑是否正在使用正確的上下文。

+0

沒有必要,一個進度對話框就可以很好地處理OP的工作。似乎有一些小問題,我認爲joaquin的解決方案會解決它。 – varunkr

相關問題