0

In my Application I am trying to simply setText() on my Button purchaseButton. I have an Alert Dialog that takes a value and initializes an AsyncTask to complete the server call to find the discount.

All of that works fine, its when I get to the onPostExecute().

onPostExecute():

protected void onPostExecute(String result) {
            Log.d(tag,"Result of POST: " + result);
            if(result != null){
                if(result.equals("NO")){
                    createAlert(1);

                }else{
                    result = result.replaceAll("YES", "");
                    String discount = result;
                    discountPrice = price - Double.parseDouble(discount);
                    Log.d(tag, "Discount price after pull:" + discountPrice);
                    //setPurchase("Purchase $" + String.valueOf(discountPrice));

                    new Thread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            Message msg = handler.obtainMessage();
                            msg.what = (int) discountPrice;
                            handler.sendMessage(msg);
                        }
                    }).start();

                }
            }

You can see that I make a call to a handler from a new Thread(). This allows me to get to the handler but never sets the text of the button from the handler.

Handler Method:

final static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            Log.d(tag, "entered handler");
            if(msg.what == discountPrice)
            {
                setPurchaseText(msg.what);
            }
        }
    };

setPurchaseText() method:

private static void setPurchaseText(int value){
        Log.d(tag, "Entered setPurchaseText");
        purchaseButton.setText("Purchase $" + String.valueOf(value));
    }

From my knowledge this should allow me to set the text from the handler. Why is it not setting the text and how can I get it to set the text with my string value?

Any help is much appreciated!

Keeano
  • 309
  • 8
  • 33
  • 1
    What for do you need this Handler anyways? `onPostExecute()` already runs in the UI Thread. – SimonSays Aug 05 '13 at 22:23
  • I have tried setText from onPost, that was my initial commands. It too will not setText.@SimonSays – Keeano Aug 05 '13 at 22:25
  • you dont need to use String.valueOf(value) just do this "Purchase $"+value); this will concatenate the integer value – Areeb Gillani Aug 05 '13 at 22:50
  • If the `setText()` wasn't working in `onPostExecute()` then the problem is elsewhere. Did you debug to see that you are getting the correct `result` from `doInBackground()`? Are you getting an error or is the text just not changing? – codeMagic Aug 05 '13 at 22:58
  • I did end up getting it to work form your comment earlier, it made me look deeper and found it in a Double.parseDouble(String). Thanks, it now works onPostExecute – Keeano Aug 05 '13 at 23:40

1 Answers1

1

If the setText() is not working in onPostExecute(), they you might not be creating your AsyncTask on the UI thread. AsyncTask runs onPostExecute() on the thread you create it.

Again, Handler is created on the currently executed Thread. May be your Handler is initialized on a background thread. Try using final static Handler handler = new Handler(Looper.getMainLooper()). This ensures the handler to be attached to the main thread.

sriramramani
  • 1,088
  • 1
  • 9
  • 8