2

I want to show AlertDialog which is in other class in AsyncTask. Example>

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..
    }

and ConfirmAlertDialog is...

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

        }
    });

}

I think g_ctx.class.runonuiThread ... but I can't call runonuithread...

How can I solve it?

Adrian
  • 301
  • 4
  • 15

2 Answers2

3

runOnUiThread method is present in Activity class. So should pass Activity into your class. Example:

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

        }
    });

}

}
an_droid_dev
  • 1,136
  • 14
  • 18
  • Thanks!! u save my time!!! It is good to me. and I'm just wondering whether It is never call error about activity.. ?? cuiz I'm changing Toast to AlertDialog... It is befor custom Toast msg = message; runOnUiThread(new Runnable() { @ Override public void run() { View view = View.inflate(Form_Main.this, R.layout.common_toast, null); TextView tv = (TextView) view.findViewById(R.id.textViewToast); tv.setText(msg); Toast t = new Toast(_context); t.setView(view); t.setDuration(Toast.LENGTH_LONG); t.setGravity(Gravity.CENTER, 0, 0); t.show(); }); – Adrian Aug 26 '16 at 08:47
  • @Adrian you get an error in this case only if activity is null, so you should also check activity if is null in top of method. – an_droid_dev Aug 26 '16 at 08:58
  • Thanks!! It is perfect to me. I resolved my problem! – Adrian Aug 29 '16 at 10:15
1

You can call it in main thread using handler

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
             // Your code here
        }
    });
Sujith Niraikulathan
  • 2,111
  • 18
  • 21