How to update a TextView from within onPostExecute

ⅰ亾dé卋堺 提交于 2020-01-11 14:38:07

问题


Need help here, about onPostExecute. If I want to put the update on textView what should be the code and what should I do?.

@Override
protected Value[] doInBackground(Integer... params) {
    ApiClient apiClient = new ApiClient(API_KEY);
    Variable batteryLevel = apiClient.getVariable(VARIABLE_ID);
    Value[] variableValues = batteryLevel.getValues();

    return variableValues;
}

@Override
protected void onPostExecute(Value[] variableValues) {
    // Update your views here
}

回答1:


Do as follows, save reference to TextView as member and use it in nested AsyncTask:

public class YourActivity extends Activity{

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activity_layout);
        textView = (TextView)findViewById(R.id.yourTextViewID);
    ...
    }

    private YourAsyncTask extends AsyncTask{
    ...
        @Override
        protected void onPostExecute(Value[] variableValues) {
            if(textView != null)
                textView.setText("your text");

         }
    }
}



回答2:


If you want get last value from Ubidots to your textview. You can try this, but u must set the textview first OnCreate

@Override
        protected void onPostExecute(Value[] variableValues) {
            // Update your views here

            String status = Double.toString(variableValues[0].getValue());
//this is textview for show last value from ubidots
            mBatteryStatus.setText(status);

        }
    }


来源:https://stackoverflow.com/questions/34248468/how-to-update-a-textview-from-within-onpostexecute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!