开发者

returning from AsyncTask

I am having problems changing a开发者_运维知识库n image in my UI depending on the return value of a boolean variable in my thread.

my class is defined as:

class recorderThread extends AsyncTask<String, Void, Boolean> {

I pass in a string, i want a boolean out. if i cant pass the boolean out then i could do this... but i tried and cant change the image from the thread as follows

@Override
protected void onPostExecute(Boolean result) {

if (result)
    ball = new Ball(getContext(),R.drawable.correctball);     
else
    ball = new Ball(getContext(),R.drawable.wrongball); 
}

Is there not a way I can get the boolean value from calling the thread in the UI?

new recorderThread().execute("A");

Thanks Guys, hope it makes sense.


There's no reason why you shouldn't be able to return a boolean from your doInBackground method of the AsyncTask. Just make sure that the definition is returning a boolean and that you're setting it appropriately for your needs:

private class recorderThread extends AsyncTask<String, Void, Boolean> {
   protected boolean doInBackground(String... strings) {
       boolean result;
       // Do your background process and make sure
       // that you set the boolean correctly
       return result;
   }

   protected void onPostExecute(boolean result) {
       // Your current UI stuff here.
   }
}

Also, put in lots of logging. Make sure the boolean is being set correctly. Also, step through it with the debugger. Should be an easy fix assuming your correctly updating the UI in your Ball() method. Note: check that too to make sure it works correctly.


Only UI Thread is allowed to modify UI (and your async task runs on separate thread), so all UI modification shall happpen via runOnUIThread() of your activity:

    activeHighscoreEntry.setTurns(activeHighscoreEntry.getTurns() + 1);
    runOnUiThread(new Runnable() {
        public void run() {
            if (!handleRemoval(point1)) {
                placeNext();
            }
        }
    });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜