将json数据显示到edittext中

时间:2014-06-26 10:51:02

标签: android json

大家好,所以我有这个函数获取json数组并将数据显示到listview中,但我想要的是将结果显示在edittext上如何修改这个可以任何人帮助我。谢谢。我知道我需要改变postexecute方法,但我不知道该怎么做

 /**
 * 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);

        }
    });

}

 }

3 个答案:

答案 0 :(得分:0)

你可以在AsyncTask对象中创建一个成员变量(JSONObject jsonOutput)并为其分配json。然后在onPostExecute中使用

EditText.setText(jsonOutput.toString)

希望有所帮助

答案 1 :(得分:0)

你可以试试这个:

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

注意:无需在runOnUiThread中使用onPostExecute(),因为onPostExecute默认情况下会在UI线程上运行。

祝你好运。

答案 2 :(得分:0)

将此内容写入onPostExecute

editText.setText(productsList.toString());