How to update Textview from Asynctask?

Asked

Viewed 1,461 times

2

I saw in this example, updating the field TextView from within the AsyncTask but I can’t repeat that in my code, and it seems to me that’s not even possible or it’s?

protected String doInBackground(String... params) {

    // updating UI from Background Thread
    runOnUiThread(new Runnable() {
        public void run() {
            // Check for success tag
            int success;
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("pid", pid));

                // getting product details by making HTTP request
                // Note that product details url will use GET request
                JSONObject json = jsonParser.makeHttpRequest(
                        url_product_detials, "GET", params);

                // check your log for json response
                Log.d("Single Product Details", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // successfully received product details
                    JSONArray productObj = json
                            .getJSONArray(TAG_PRODUCT); // JSON Array

                    // get first product object from JSON Array
                    JSONObject product = productObj.getJSONObject(0);

                    // product with this pid found
                    // Edit Text
                    txtName = (EditText) findViewById(R.id.inputName);
                    txtPrice = (EditText) findViewById(R.id.inputPrice);
                    txtDesc = (EditText) findViewById(R.id.inputDesc);

                    // display product data in EditText
                    txtName.setText(product.getString(TAG_NAME));
                    txtPrice.setText(product.getString(TAG_PRICE));
                    txtDesc.setText(product.getString(TAG_DESCRIPTION));

                }else{
                    // product with pid not found
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

    return null;
}

My Code!

protected String doInBackground(String... params) {

    runOnUiThread(new Runnable() {      
        public void run() {
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("id", pid));   

                //Conexao
                JSONObject json = jsonParser.makeHttpRequest(Extras.urlListarProdutosID(), "POST", param);

                  try {
                        int success = json.getInt("sucesso");
                        if (success == 1) {
                            JSONArray productObj = json.getJSONArray("produto");
                            JSONObject produto = productObj.getJSONObject(0);

                            nome = (EditText) findViewById(R.id.nome);                      

                            nome.setText(produto.getString("nome"));                     

                        }                                       

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    });
    return null;
}

3 answers

4


The official documentation of Android is here

Asynchronous Task is defined as a computation that runs on a Background Thread and that the result is published in the Interface Thread (UI Thread). On Android we have 3 generic types, called Params, Progress and Result, and 4 steps called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.

Generic Types of Asynctask

  • Params - Type of parameter sent to task
  • Progress - Type of progress unit parameter to be used in the task
  • Result - Type of task output

Not all parameters are used in Task, to ignore a parameter use the class Void

private class MyTask extends AsyncTask<Void, Void, Void> { ... }

Four steps

When the task is executed it goes through 4 steps:

  • onPreExecute() - Invoked in the Thread UI before execution.

  • doInBackground(Params...) - Invoked after onPreExecute() and runs in a separate Thread.

  • onProgressUpdate(Progress...), Invoked in the Thread UI after the publishProgress(Progress...) method is called inside theInBackground(...)

  • onPostExecute(Result), Invoked after Ackground ends(...)

For an XML like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText"/>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Task"
            android:id="@+id/button"/>
</LinearLayout>

You could do the Asynctask to update like this:

public class MyActivity extends Activity {

    private EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.editText = (EditText) findViewById(R.id.editText);
        findViewById(R.id.button).setOnClickListener(getOnClickButton());
        Log.d("test","onCreate");
    }

    private View.OnClickListener getOnClickButton() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("test","onClick");
                startTask();
            }
        };
    }

    private void startTask() {
        new Task().execute();
    }
    private class Task extends AsyncTask<Void, Void, Void> {
        private String text;

        @Override
        protected Void doInBackground(Void... voids) {
            Log.d("test","doInBackground");
            this.text = "Xubaduba";
            return null;
        }

        protected void onPostExecute(Void result) {
            Log.d("test","onPostExecute");
            editText.setText(this.text);
        }
    }
}

In Log you would get something like:

inserir a descrição da imagem aqui

  • Man Thank you so much for your explanation!

  • Just mark as ok the answer Marcelo. :)

3

When a AsyncTask runs through 4 times:

onPreExecute() - Runs in the thread UI and before the task is run.

doInBackground(Params...) - It is executed in the background thread shortly after onPreExecute()

onProgressUpdate(Progress...) - Runs in the thread UI when invoked by publishProgress(Progress...), This is where you should update the View during the course of AsyncTask.

onPostExecute(Result) - Runs in the thread UI right after the AsyncTask have finished. Here you can also update the View

For a full explanation see Asynctask

1

What happens is that you are trying to update your View within the Ackground() method that runs on another thread and this is not allowed.

You can update your View in the following Asynctask methods: publishProgress(), onPreExecute() and onPostExecute(). For this, within some of these methods you add the excerpt below:

EditText nome = (EditText) findViewById(R.id.nome);                      
nome.setText(produto.getString("nome"));  

Getting something like:

@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);
    EditText nome = (EditText) findViewById(R.id.nome);                      
    nome.setText(produto.getString("nome"));  
}

Simple!

  • I understood that I saw it that way, I thought it was possible! Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.