2012-01-28 34 views
1

我使用異步任務顯示進度對話框。本地化進度對話框

private class RetrieveTask extends AsyncTask<String, Void, String> 
{ 
    RetrieveTask t = this; 
    ProgressDialog d = null; 

    @Override 
    protected void onPreExecute() 
    { 
     // TODO 
     //d = ProgressDialog.show(context, "", context.getString(R.string.querying_server), true, true, new DialogInterface.OnCancelListener() { 
     d = ProgressDialog.show(context, "", "Loading...", true, true, new DialogInterface.OnCancelListener() { 
       public void onCancel(DialogInterface dialog) { 
       t.cancel(true); 
     } 
     }); 
    // continues.... 

通過這一階段context已被設置爲根活動。

我想按照「TODO」後面的註釋中的說明對本消息進行本地化。爲此,我需要獲取字符串資源;我如何從這裏最好地做到這一點?

回答

2

我缺少一個getResources()電話,僅此而已。

context.getResources().getString(R.string.my_string_id);

0

我只是使用對調用活動上下文的引用。

示例僞代碼:

public class MyMainActivity extends Activity { 

    private void MethodA{ 
     new RetreiveTask().execute(); 
    } 

    //then in the AsyncTask, you could get a context by using the: *MyMainActivity.this* syntax. 

    private class RetrieveTask extends AsyncTask<String, Void, String> 
    { 
     RetrieveTask t = this; 
     ProgressDialog d = null; 

     @Override 
     protected void onPreExecute() 
     { 
      // TODO 
      //d = ProgressDialog.show(context, "", context.getString(MyMainActivity.this.R.string.querying_server), true, true, new DialogInterface.OnCancelListener() { 
      d = ProgressDialog.show(context, "", "Loading...", true, true, new DialogInterface.OnCancelListener() { 
        public void onCancel(DialogInterface dialog) { 
        t.cancel(true); 
      } 
      }); 

    } 



    } 
+0

謝謝。然而,這項任務不是在它意識到的活動中進行的。 – SK9 2012-01-29 08:40:55