这一个已经啃了我一会儿,我创建了一个AsyncTask来从我的数据库中提取产品,我希望它们能够显示在listView中,不会出现构建错误,并且LogCat会打印出来JSON的代码,但没有打印到ListViewJSON不打印到ListView
private class LoadJsonTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>> > {
ProgressDialog dialog ;
protected void onPreExecute() {
dialog = ProgressDialog.show(GetProducts.this ,"Loading","Please wait");
}
protected ArrayList<HashMap<String, String>> doInBackground (String... params) {
return doGetJson();
}
protected void onPostExecute(ArrayList<HashMap<String, String>> mylist) {
dialog.dismiss();
}
}
public ArrayList<HashMap<String, String>> doGetJson() {
JSONObject json = null;
ArrayList<String> mylist = new ArrayList<String>();
Functions uf = new Functions();
json = uf.getProducts();
try {
//the array title that you parse
JSONArray category = json.getJSONArray("products");
ArrayList<String> items = new ArrayList<String>();
for(int i=0; i < category.length() ; i++) {
JSONObject c = category.getJSONObject(i);
String name = c.getJSONObject("name").toString();
String products = c.getJSONObject("price").toString();
items.add(name);
items.add(products);
Log.d(name,"Output");
}
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, items);
ListView ProductList = (ListView)findViewById(R.id.listView1);
mArrayAdapter.notifyDataSetChanged();
ProductList.setAdapter(mArrayAdapter);
} catch (JSONException e) { }
return null;
}
的logcat的打印出
1-05 23:08:09.732: E/JSON(17841): {"tag":"getProducts","success":1,"error":0,"products":{"name":"Coffee1","price":"4.00","image":"http:\/\/test.com"}}{"tag":"getProducts","success":1,"error":0,"products":{"name":"Coffee2","price":"5.00","image":"http:\/\/test2.com"}}
但正如我说什么被打印到我的ListView它只是保持空白。
您好,感谢回答。当你说“那个代码”时,你的意思是for循环还是for循环之后的所有内容? – user2960452
我更新了我的答案,以便更具体。 –
嗨,当我移动到onPostExceute它告诉我,“项目”无法找到 - doInBackground需要返回项目吗? – user2960452