2014-01-05 32 views
0

这一个已经啃了我一会儿,我创建了一个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它只是保持空白。

回答

0

,你创建ArrayAdapter,并设置它的ListView应该在UI线程上执行(从onPostExecute)代码:

ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, items); 
    ListView ProductList = (ListView)findViewById(R.id.listView1); 
    ProductList.setAdapter(mArrayAdapter); 

你不能这样做在后台线程这项工作。

您已经返回了信息的HashMap,因此您只需将该代码移出doInBackgroundonPostExecute即可。

此外,您可以删除对notifyDataSetChanged的调用,因为这是初始数据加载。所以我会做这些改变:

  • 你的AsyncTask应返回的ArrayList:更换无处不在,你有HashMap<String, String>ArrayList<String>
  • 后,在doGetJson循环,return items
  • 到onPostExecute的参数是现在ArrayList<String>(从步骤1),现在可以使用此参数来构建适配器。
+0

您好,感谢回答。当你说“那个代码”时,你的意思是for循环还是for循环之后的所有内容? – user2960452

+0

我更新了我的答案,以便更具体。 –

+0

嗨,当我移动到onPostExceute它告诉我,“项目”无法找到 - doInBackground需要返回项目吗? – user2960452

0
String[] itemsArray = items.toArray(new String[items.size()]); // convert list to array 
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, itemsArray); 
ListView ProductList = (ListView)findViewById(R.id.listView1); 
ProductList.setAdapter(mArrayAdapter); // swap the last two lines like this 
mArrayAdapter.notifyDataSetChanged(); 

即时猜测这应该解决您的问题

更新代码

+0

这给了我与上述相同的问题没有找到项目,并返回它生成错误 – user2960452

+0

所以,项目列表中有什么? –

+0

是的上面的代码中的for循环添加到项目列表中的东西 – user2960452