Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将json数据显示到EditText中_Android_Json - Fatal编程技术网

Android 将json数据显示到EditText中

Android 将json数据显示到EditText中,android,json,Android,Json,大家好,我有一个函数,它获取一个json数组并将数据显示到listview中,但我想要的是将结果显示在edittext上。我如何修改它?有人能帮我吗。谢谢我知道我需要修改postexecute方法,但我不知道怎么做 /** * Background Async Task to Load all product by making HTTP Request * */ class LoadAllProducts extends AsyncTask<String, String, Stri

大家好,我有一个函数,它获取一个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);

        }
    });

}

 }
/**
*通过发出HTTP请求加载所有产品的后台异步任务
* */
类LoadAllProducts扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
/**
*从url获取所有产品
* */
受保护的字符串doInBackground(字符串…args){
//建筑参数
试一试{
List params=new ArrayList();
参数add(新的BasicNameValuePair(“id”,teste));
//从URL获取JSON字符串
JSONObject json=jParser.makeHttpRequest(url_all_products,“POST”,params);
//检查日志cat中的JSON响应
Log.d(“所有产品:,json.toString());
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//获取一系列产品
products=json.getJSONArray(“estab”);
//在所有产品中循环
对于(int i=0;ivalue
地图放置(标签设计,设计);
//将哈希列表添加到ArrayList
productsList.add(地图);
} 
}否则{
//没有发现任何产品
//启动添加新产品活动
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//获取所有产品后关闭对话框
pDialog.disclose();
//从后台线程更新UI
runOnUiThread(新的Runnable(){
公开募捐{
/**
*将解析的JSON数据更新到ListView中
* */
适配器=新的SimpleAdapter(
Linhas_Pesage.这个,产品列表,
R.layout.list_lin_项,新字符串[]{TAG_ID,
TAG_ESTAB,TAG_DATA,TAG_HORA,TAG_QTD,TAG_IDESTAB},
新的int[]{R.id.id,R.id.id_estab,R.id.dt,R.id.hr,R.id.quantidade,R.id.idestab});
//更新列表视图
setListAdapter(适配器);
}
});
}
}

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

EditText.setText(jsonOutput.toString)
希望对您有所帮助

您可以尝试以下方法:

EditText editText = (EditText)findViewById(R.id.edit_text); //in your onCreate();
.
.
.
editText.setText(productsList.toString()); //in onPostExecute();
注意:无需在
onPostExecute()
中使用
runOnUiThread
,因为默认情况下
onPostExecute
在UI线程上运行


祝你好运。

在onPostExecute中写下这个

editText.setText(productsList.toString());

在我的代码中,我是说base放在postexecute上,执行这个actv.setText(products.toString());是的,在PostExecute中设置文本,因为它必须在UI线程中完成。这取决于您希望在edittext中输入的内容-但是如果您希望从URL获得完整的JSON输出,请将其分配给变量,并使用toString将其转储到postExec中的edittext