0

I am currently working on project. It involves reading data from cloud server continuously. I am using ubidots server. Currently i have created class that extends to asynctask class like this

public class ApiUbidots extends AsyncTask<Integer, Void, Void> {
    private final String API_KEY = "1XXXXXXXXX";

    private static final String tempID = "56XXXXXXXX";

    @Override
    protected Void doInBackground(Integer... params) {
        final ApiClient apiClient = new ApiClient(API_KEY);

        Variable tempVariable = apiClient.getVariable(tempID);

        Value[] tempValues = tempVariable.getValues();
        Double vlStr = tempValues[0].getValue();
        String tempValue = String.valueOf(vlStr);
        Log.i(TAG, "TEMPERATURE VALUE IS ====" + tempValue);

        tempTextView.setText(tempValue);

        return null;
    }
}

now i am trying to set the values i got from server to display on textview but i cannot set in this class.

I need to find a way where the textview have to be changed whenever the data variable changes

Can anyone please help me how to proceed to get it working. Thank you!

1 Answer 1

1

If you have this class defined as a subclass, all you need to do is update your TextView in the onPostExecute() method of your AsyncTask. If this task is a class in a separate file, you would define a callback to send data back to the calling Activity, then update your TextView with the data you receive in the callback. Here's some example code:

Your AsyncTask would look something like this. Notice it defines a public interface that acts as a callback for the results of the task.

import android.os.AsyncTask;

public class TestTask extends AsyncTask<String, Void, String> {

    TestTaskCallback listener;

    public TestTask(TestTaskCallback listener) {
        this.listener = listener;
    }

    protected String doInBackground(String... args) {

        String input = args[0];
        String output = "simulated return value";

        return output;
    }

    protected void onPostExecute(String result) {
        listener.onResultReceived(result);
    }

    public interface TestTaskCallback {
        void onResultReceived(String result);
    }
}

Then your calling Activity would implement the callback, fire the Task, then wait for the result, like this:

public class TestActivity extends AppCompatActivity implements TestTask.TestTaskCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        new TestTask(this).execute("Some input");

    }

    public void onResultReceived(String result) {
        Log.d("TEST TASK RESULT", result);
    }
}

Once the result comes in, you can use it in your Activity to update your TextView. And as I mentioned above, this is all way easier if you just subclass the Task right in the Activity itself. That way, you'll just have access to your TextViews right from the Task. No need to mess with callbacks at that point.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.