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!