2014-06-26 54 views
0

大家好,我有這個函數獲取一個json數組並將數據顯示到一個listview中,但我想要的是將結果顯示在edittext上如何修改此任何人都可以幫助我。謝謝。我知道我需要改變的postexecute方法,但我不知道如何做到這一點將json數據顯示到editext中

/** 
* Background Async Task to Load all product by making HTTP Request 
* */ 
class LoadAllProducts extends AsyncTask<String, String, String> { 

/** 
* Before starting background thread Show Progress Dialog 
* */ 
@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 

} 

/** 
* getting All products from url 
* */ 
protected String doInBackground(String... args) { 
    // Building Parameters 


    try { 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("id", teste)); 
     // getting JSON string from URL 
     JSONObject json = jParser.makeHttpRequest(url_all_products, "POST", params); 

     // Check your log cat for JSON reponse 
     Log.d("All Products: ", json.toString()); 
     // Checking for SUCCESS TAG 
     int success = json.getInt(TAG_SUCCESS); 

     if (success == 1) { 
      // products found 
      // Getting Array of Products 
      products = json.getJSONArray("estab"); 


      // looping through All Products 
      for (int i = 0; i < products.length(); i++) { 
       JSONObject c = products.getJSONObject(i); 

       // Storing each json item in variable 
       String design = c.getString(TAG_DESIGN); 

       // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 
       // adding each child node to HashMap key => value 
       map.put(TAG_DESIGN, design); 


       // adding HashList to ArrayList 
       productsList.add(map); 


      } 
     } else { 
      // no products found 
      // Launch Add New product Activity 

     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 

/** 
* After completing background task Dismiss the progress dialog 
* **/ 
protected void onPostExecute(String file_url) { 
    // dismiss the dialog after getting all products 
    pDialog.dismiss(); 
    // updating UI from Background Thread 
    runOnUiThread(new Runnable() { 
     public void run() { 
      /** 
      * Updating parsed JSON data into ListView 
      * */ 
     adapter = new SimpleAdapter(
        Linhas_pesagem.this, productsList, 
        R.layout.list_lin_items, new String[] { TAG_ID, 
          TAG_ESTAB, TAG_DATA, TAG_HORA, TAG_QTD, TAG_IDESTAB}, 
        new int[] { R.id.id, R.id.id_estab, R.id.dt, R.id.hr, R.id.quantidade, R.id.idestab}); 
      // updating listview 
      setListAdapter(adapter); 

     } 
    }); 

} 

} 

回答

0

你可以讓一個成員變量(JSONObject的jsonOutput)中的AsyncTask對象和JSON分配給它。然後在onPostExecute使用

EditText.setText(jsonOutput.toString) 

希望它有助於

+0

y的話說基地在我的代碼穿上postexecute此\t actv.setText(products.toString()); – user3734013

+0

是在PostExecute中設置文本,因爲它必須在UI線程中完成。 – siliconeagle

+0

它取決於你想放在edittext中的內容 - 但是如果你想從URL中得到完整的JSON輸出,那麼將它分配給一個變量並使用toString將它轉儲到postExec中的EditText中 – siliconeagle

0

你可以試試這個:

EditText editText = (EditText)findViewById(R.id.edit_text); //in your onCreate(); 
. 
. 
. 
editText.setText(productsList.toString()); //in onPostExecute(); 

注意:沒有必要在onPostExecute()使用runOnUiThread,因爲onPostExecute在默認情況下,UI線程中運行。

祝你好運。

0

寫這篇文章onPostExecute

editText.setText(productsList.toString());