Asynctask For Update Button
How to update my UI button for disabled button on/off if i have condition. The condition : if i get value = 1.0 the button power on is disabled & and if i get value 0 the butto
Solution 1:
you have initialize
your button in onCreateView
like
ImageButton On = (ImageButton)rootView.findViewById(R.id.on);
and trying to access it in onPostExecute().they aren't global.
And to disable you have to call setEnable(false)
not setVisibility
And you can also send it as Rathna Kumaran said.
public class GetApi extends AsyncTask<Object, Object, Value[]> {
private Button button;
public GetApi(Button mButton){
this.button = button;
}
private final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
private final String SWITCHID = "xxxxxxxxxxxxxxxxxxxxxxxxx";
@Override
protected Value[] doInBackground(Object... params) {
ApiClient apiClient = new ApiClient(API_KEY);
Variable statusSwitch = apiClient.getVariable(SWITCHID);
Value[] variableValues = statusSwitch.getValues();
return variableValues;
}
@Override
protected void onPostExecute(Value[] variableValues) {
String status = Double.toString(variableValues[0].getValue());
mSwitchStatus.setText(status);
if(status.equals("1.0")){
button.setVisibility(View.INVISIBLE);
}
}
}
Solution 2:
1.Create a constructor in a AsyncTask and send the button as parameter. ex. getApi(buttonOne).execute();
Inside a getApi() constructor declare a local variable "Button" and assigned to that.
In onPostExecute() -> buttonOne.setVisibility(View.INVISIBLE).
In asyncTask , "onPostExecute()" will perform in a main thread. UI elements should only get update in a main thread. So that passing a UI element into an Asynctask and it can be update inside the onPostExecute.
Post a Comment for "Asynctask For Update Button"