0

I am puzzled with how to use Async. I am wanting it to update the UI as soon as a button is clicked without having to kill the activity and re instantiate it. In the background, I want it to be waiting for the click to update the UI, and after the click I want it to determine whether or not the correct button was clicked. I am using the boolean isUserRight(); to determine whether or not the answer is correct, and after that I am using resetButtons(); to reset the buttons (obviously) and then setButtons(); to change the button. I have the methods all written out, I just don't know how to implement them into Async. I need the UI to be updated instantly to change the color of a button. I don't need help with any of the methods, button color, etc, just the Async, and I'm not asking anybody to spoonfeed me either, just give me a little push on where to go. Any help would be nice, thanks guys!

TL;DR need to update UI after button is pushed using async so the button color doesn't get stuck and I don't have to restart the activity to update the UI. I don't need the whole code, just a little guidance on where to go.

Edit: Added some code.

This is on my onCreate();

buttons[0] = (Button)findViewById(R.id.b0);
    buttons[1] = (Button)findViewById(R.id.b1);
    buttons[2] = (Button)findViewById(R.id.b2);
    buttons[3] = (Button)findViewById(R.id.b3);
    buttons[0].setOnClickListener(this);
    buttons[1].setOnClickListener(this);
    buttons[2].setOnClickListener(this);
    buttons[3].setOnClickListener(this);

then under my onClick, I have a switch for the buttons which all go to this method:

    public boolean isUserRight() {
        if(correct == 1) {
              onButton();
            return true;
            //Toast.makeText(getApplicationContext(), "Good job!", Toast.LENGTH_SHORT).show();
            //buttons[test].getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
        }
        onButton();
        return false;
    }


    public void onButton() {
        int rnd = new Random().nextInt(buttons.length);
        test = rnd;
        buttons[rnd].getBackground().setColorFilter(Color.YELLOW, PorterDuff.Mode.MULTIPLY);
        //ResetButtons();
    }

And this works fine, the only problem is after the first button being clicked, the colors stop changing and getting updated.

Tyson
  • 85
  • 7
  • http://developer.android.com/reference/android/os/AsyncTask.html – Raghunandan Jan 26 '14 at 02:13
  • 1
    If I get it correctly, you shouldn't have to use Async to do what you want. It would be useful if you add some code to your question. Anyway did you set an OnClickedListener on your button, and are you updating the UI in that? – Lauw Jan 26 '14 at 02:14
  • 1
    Yes, I put an listener on the button. and my problem is I change the color of the button, but the UI doesn't get updated so the button color stays the color it was before until you hit another button, basically it's a step behind. I just need something to refresh the UI instantly, so the color gets changed instantly. – Tyson Jan 26 '14 at 03:21
  • Post that code "Yes, I put an listener on the button. and my problem is I change the color of the button, but the UI doesn't get updated". Because Lauw is correct. You must be doing something wrong, or there is something you're not telling us. And we won't know what's actually happening until you post your code. Using AsyncTask for something like that is overkill. It shouldn't be needed. – Stephan Branczyk Jan 26 '14 at 03:51
  • @StephanBranczyk There you go. Updated the post with some code, hopefully that will help. – Tyson Jan 26 '14 at 04:31
  • So if I understand your question correctly, the color of a button does change once, but then it doesn't change again the second time you click another button? Correct? – Stephan Branczyk Jan 26 '14 at 23:24
  • @StephanBranczyk Yes sir. I am pretty sure it is because the UI isn't being updated. When the onButton is called, it doesn't visually reset the buttons, but it does in the code. I put a debug on it and tested it, and the color was getting set, just not visually changing. – Tyson Jan 26 '14 at 23:47
  • Please share more of your code. It's a bit unusual, I'm having trouble understanding it. For one thing, (correct == 1) is most likely wrong. In Java, if you start interchanging boolean values with integers, you'll get into trouble. See http://stackoverflow.com/questions/13806021/checking-the-boolean-result-of-an-int-type – Stephan Branczyk Jan 27 '14 at 02:23
  • @StephanBranczyk I fixed it, you were right, async was overkill so I ended up using invalidate and just setting it to all the buttons. thanks for your help, it was greatly appreciated. – Tyson Jan 27 '14 at 02:24

2 Answers2

1
void test(){
    final Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            if(msg.what == 0){
                // update your ui

            }

        }

    };

    new Thread(new Runnable() {
        public void run() {
            // do network job
            // do something you want here
            mHandler.sendEmptyMessage(0);
        }
    }).start();

}
Kelvin Fu
  • 24
  • 2
1

Alright, I fixed it. Thanks for all your help, to anybody who was wondering how I fixed it, I was using view.invalidate, which wasn't directed to anything so I had to change it to the buttons array ex.:

buttons[1].invalidate(); and that would refresh the buttons instead of the entire activity.

Tyson
  • 85
  • 7