开发者

How to run multiple AsyncTask?

I have 2 spinner, each spinner's data loaded from database using AsyncTask

i call the AsyncTasks using this

new PopulateSpinnerA().execute();

it works if i only call one AsyncTask for one Spinner

BUT!

i have 2 Spinners, so i call the AsyncTask for each Spinner like this

new PopulateSpinnerA().execute(); // for Spinner A
new PopulateSpinnerB().execute(); // for Spinner B

I run it and my app force close

solution?

UPDATE!

i get inspiration from someone below who answer with true and false

im us开发者_如何学编程ing a boolean (playing with true and false) to make my two spinners generated

first i make a boolean variable

Boolean SPN = false;

then i make a function to check the boolean and put it on onCreate() function

private void cek(){
        if(!SPN){
            new populateSpinnerA().execute();
        }
        if(SPN){
            new populateSpinnerB().execute();
        }
    }

on populateSpinnerA() i just put this 2 lines to run the second spinner's AsyncTask

SPN = true;
cek();

and

BOOM!

it's done :D


You can not have two spinner at a time. Need to use any trick in this case,

  1. Use only one spinner.
  2. Start the spinner while initiating first spinner.
  3. Use one common flag set on PostExecute.
  4. Before step#3, on postExecute of both AsyncTask check the flag is already set, if yes just cancel the spinner.

Refer below pseudo code.

postExecute(){
      If(taskCompletedFlag == true){
            //Code to cancel the spinner.
            taskCompletedFlag = false;
      }else{
            taskCompledtedFlag = true;
      }
}

P.S. - In case you are not aware which AsyncTask will initiate first, you can use same mechanism over there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜