如何在AsyncTask完成后showPopupWindow?

时间:2018-03-22 16:24:41

标签: android android-asynctask popupwindow

我想在单击View时在弹出窗口中显示textView,但计算需要时间,所以我在AsyncTask中进行计算,但是如何在AsyncTask进程完成后立即显示弹出窗口?

public void onClick(View widget) {

        MyAsyncTask asyncTask = new MyAsyncTask(new AsyncResponse() {
            @Override
            public void processFinish(Object output) {
            meaning_result = (String) output;
            }
            });
         asyncTask.execute("xxxxx");
         showPopupWindow(widget);
}

这是我的第一个想法,但showPopupWindow(widget)首先执行,meaning_result尚未分配。如果分配了meaning_result,如何使showPopupWindow(widget)运行?

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

public void showPopUp(final View widget){
         runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showPopupWindow(widget);
            }
        });
}

和:

public void onClick(final View widget) {

        MyAsyncTask asyncTask = new MyAsyncTask(new AsyncResponse() {
            @Override
            public void processFinish(Object output) {
                meaning_result = (String) output;
                showPopUp(widget);
            }
        });
        asyncTask.execute("xxxxx");

    }

希望这有帮助

答案 1 :(得分:0)

您需要在AsynTask的postExecute中显示弹出窗口。

public void onClick(View widget) {

     AsyncTask asyncTask = new AsyncTask(){

        @Override
        protected long doInBackground(){
            //Do your cacultation
        }

        @Override
        protected void onPostExecute(Long result) {
             showPopupWindow(widget);
        }

        });
     asyncTask.execute("xxxxx");
}