成员变量如下:
private ListView mListView;
private ArrayList<ShopCartPo> mGoods;
private ShoppingCartAdapter mAdapter;
mListView = (ListView) this.findViewById(R.id.id_lv_shopping_cart);
mGoods = new ArrayList<ShopCartPo>();
mAdapter = new ShoppingCartAdapter(this, mGoods, R.layout.shoppingcart_item);
错误赋值:
mGoods = gson.fromJson(response, listType);//此处解析返回的是一个ArrayList对象
mAdapter.notifyDataSetChanged();
原因:因为notifyDataSetChanged()更新的是原数据对象,此处相当于重新给mGoods一个地址,也就是说
mAdapter = new ShoppingCartAdapter(this, mGoods, R.layout.shoppingcart_item);里面的mGoods是没有改变的,依然是空。
正确做法:
mGoods.addAll((ArrayList<ShopCartPo>) gson.fromJson(response, listType));
mAdapter.notifyDataSetChanged();