AlertDialog setmessage not working inside Asynctask

孤街浪徒 提交于 2019-12-06 05:05:57

As per your code, alert is an AlertDialog.Builder and not an AlertDialog itself. This raised a concern to me because the reason it might not be changing is because you already showed the builder, but not give to an AlertDialog. So I tried out a simple code:

public class MainActivity extends AppCompatActivity {

    private AlertDialog.Builder alert;
    private AlertDialog ad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alert = new AlertDialog.Builder(this);
        alert.setTitle("Downloading..");
        alert.setMessage("1");
        alert.setCancelable(false);
        ad = alert.show();


        Log.d("SAMPLE", "SET MESSAGE 2");
        alert.setMessage("2");

        Log.d("SAMPLE", "SET MESSAGE 3");
        ad.setMessage("3");
    }

}

At first, I just used the alert.setMessage (this is the AlertDialog.Builder), and the message did not change at all. But after putting it in an AlertDialog and then setting the message of the AlertDialog instance, the message changed. Care to try this approach out. Pass the AlertDialog.Builder to an AlertDialog first then, setMessage using the AlertDialog instance.

Docs for AlertDialog and AlertDialog.Builder.

Hope this helps somehow. Good luck. :)

In your code you forget to build alert dialog. See this

AlertDialog alertDialog = alert.create();
    alertDialog.show();

you have to create a alert dialog from private AlertDialog.Builder alert;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!