Skip to main content

Como atualizar TextView a partir da AsyncTask?

Vi neste exemplo, atualizando o campo TextView de dentro da AsyncTask mas não consigo repetir isso no meu código, e ao que me parece isso nem é possível ou é?

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;
}

Meu Código!

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;
}
Marcelo
  • 101
  • 4
  • 13