2016-08-26 33 views
2

我想顯示在AsyncTask中的其他類中的AlertDialog。 例>有沒有辦法在AsyncTask中顯示AlertDialog?

public class WardListAsyncTask extends AsyncTask<HashMap<String, String>, String, Object> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected Object doInBackground(HashMap<String, String>... params) { 
     ... 
    } 

    @Override 
    protected void onPostExecute(Object result) { 
     ConfirmAlertDialog(ez_WardList.this,"HI?"); 
     //this method is in another Class & I want to use this method another Asynctask also.. 
    } 

和ConfirmAlertDialog是...

Context g_ctx; 
String g_content; 
public void ez_ConfirmAlertDialog(Context ctx, String content) { 
    this.g_ctx=ctx; 
    this.g_content=content; 

    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT); 
      builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm), 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 

         } 
        }); 

      AlertDialog alertDialog = builder.create(); 
      alertDialog.show(); 
      alertDialog.getWindow().setLayout(displayWidth/2, LayoutParams.WRAP_CONTENT); 
      alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239)); 

     } 
    }); 

} 

我覺得g_ctx.class.runonuiThread ......但我不能叫runonuithread ...

我怎麼能解決這個問題?

+0

調用'#Context.runonuithread'。 – Sanoop

+0

相關:http://stackoverflow.com/questions/13974661/runonuithread-vs-looper-getmainlooper-post-in-android – Sufian

回答

3

runOnUiThread方法存在於Activity類調用它的主線程。所以應該將Activity傳遞給你的班級。 示例:

public class MyClass{ 


public void ez_ConfirmAlertDialog(Activity activity, String content) { 
    this.g_ctx=ctx; 
    this.g_content=content; 

    if(activity == null){ 
     return; 
    } 

    activity.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT); 
      builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm), 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 

         } 
        }); 

      AlertDialog alertDialog = builder.create(); 
      alertDialog.show(); 
      alertDialog.getWindow().setLayout(displayWidth/2, LayoutParams.WRAP_CONTENT); 
      alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239)); 

     } 
    }); 

} 

} 
+0

謝謝! ü節省我的時間!這對我很好。我只是想知道它是否從來沒有關於活動的錯誤。 cuiz我正在改變敬酒AlertDialog ...這是befor定製吐司 msg =消息; runOnUiThread(新的Runnable(){ @覆蓋 公共無效的run(){ 視圖視圖= View.inflate(Form_Main.this,R.layout.common_toast,NULL); TextView的TV =(TextView的)view.findViewById( R.id.textViewToast); tv.setText(MSG); 吐司噸=新吐司(_context); t.setView(視圖); t.setDuration(Toast.LENGTH_LONG); t.setGravity(重力.CENTER,0,0); t.show(); }); – Adrian

+0

@Adrian在這種情況下只有在活動爲空時纔會出現錯誤,因此如果在方法頂部爲null,則還應該檢查活動。 –

+0

謝謝!這對我來說是完美的。我解決了我的問題! – Adrian

1

可以使用處理器

new Handler(Looper.getMainLooper()).post(new Runnable() { 
     @Override 
     public void run() { 
      // Your code here 
     } 
    }); 
相關問題