我點(diǎn)了沒反應(yīng)是不是跟這個(gè)mContext為null有關(guān)我的CartProvider類public class CartProvider { ? ?public static final String JSON_CART = "json_cart"; ? ?private ?Context mContext; ? ?//優(yōu)化過的HashMap集合 ? ?private ?SparseArray<GoodsBean> datas; ? ?private static CartProvider cartProvider; ? ?private ?CartProvider(Context mContext) { ? ? ? ?this.mContext = mContext; ? ? ? ?datas = new SparseArray<>(100); ? ? ? ?listToSparse(); ? ?} ? ?public static CartProvider getInstance() { ? ? ? ?if (cartProvider == null) { ? ? ? ? ? ?cartProvider = new CartProvider(MyApplication.getContext()); ? ? ? ?} ? ? ? ?return cartProvider; ? ?} ? ?private void listToSparse() { ? ? ? ?List<GoodsBean> carts = getAllData(); ? ? ? ?//放到sparseArry中 ? ? ? ?if (carts != null && carts.size() > 0) { ? ? ? ? ? ?for (int i = 0; i < carts.size(); i++) { ? ? ? ? ? ? ? ?GoodsBean goodsBean = carts.get(i); ? ? ? ? ? ? ? ?datas.put(Integer.parseInt(goodsBean.getProduct_id()), goodsBean); ? ? ? ? ? ?} ? ? ? ?} ? ?} ? ?private List<GoodsBean> parsesToList() { ? ? ? ?List<GoodsBean> carts = new ArrayList<>(); ? ? ? ?if (datas != null && datas.size() > 0) { ? ? ? ? ? ?for (int i = 0; i < datas.size(); i++) { ? ? ? ? ? ? ? ?GoodsBean shoppingCart = datas.valueAt(i); ? ? ? ? ? ? ? ?carts.add(shoppingCart); ? ? ? ? ? ?} ? ? ? ?} ? ? ? ?return carts; ? ?} ? ?public List<GoodsBean> getAllData() { ? ? ? ?return getDataFromLocal(); ? ?} ? ?//本地獲取json數(shù)據(jù),并且通過Gson解析成list列表數(shù)據(jù) ? ?public List<GoodsBean> getDataFromLocal() { ? ? ? ?List<GoodsBean> carts = new ArrayList<>(); ? ? ? ?//從本地獲取緩存數(shù)據(jù) ? ? ? ?String saveJson = CacheUtils.getString(mContext, JSON_CART); ? ? ? ?if (!TextUtils.isEmpty(saveJson)) { ? ? ? ? ? ?//把數(shù)據(jù)轉(zhuǎn)換成列表 ? ? ? ? ? ?carts = new Gson().fromJson(saveJson, new TypeToken<List<GoodsBean>>() {}.getType()); ? ? ? ?} ? ? ? ?return carts; ? ?} ? ?public ?void addData(GoodsBean cart) { ? ? ? ?//添加數(shù)據(jù) ? ? ? ?GoodsBean tempCart = datas.get(Integer.parseInt(cart.getProduct_id())); ? ? ? ?if (tempCart != null) { ? ? ? ? ? ?tempCart.setNumber(tempCart.getNumber() + cart.getNumber()); ? ? ? ?} else { ? ? ? ? ? ?tempCart = cart; ? ? ? ? ? ?tempCart.setNumber(cart.getNumber()); ? ? ? ?} ? ? ? ?datas.put(Integer.parseInt(tempCart.getProduct_id()), tempCart); ? ? ? ?commit();//保存數(shù)據(jù) ? ?} ? ?//保存數(shù)據(jù) ? ?private void commit() { ? ? ? ?//把parseArray轉(zhuǎn)換成list ? ? ? ?List<GoodsBean> carts = parsesToList(); ? ? ? ?//把轉(zhuǎn)換成String ? ? ? ?String json = new Gson().toJson(carts); ? ? ? ?// 保存 ? ? ? ?CacheUtils.putString(mContext, JSON_CART, json); ? ?} ? ?public void deleteData(GoodsBean cart) { ? ? ? ?//刪除數(shù)據(jù) ? ? ? ?datas.delete(Integer.parseInt(cart.getProduct_id())); ? ? ? ?//保存數(shù)據(jù) ? ? ? ?commit(); ? ?} ? ?public void updataData(GoodsBean cart) { ? ? ? ?//修改數(shù)據(jù) ? ? ? ?datas.put(Integer.parseInt(cart.getProduct_id()), cart); ? ? ? ?//保存數(shù)據(jù) ? ? ? ?commit(); ? ?} ? ?/** ? ? * 根據(jù)Key查找書籍 ? ? * ? ? * @param goods_bean ? ? * @return ? ? */ ? ?public GoodsBean findData(GoodsBean goods_bean) { ? ? ? ?GoodsBean goodsBean = datas.get(Integer.parseInt(goods_bean.getProduct_id())); ? ? ? ?if (goodsBean != null) { ? ? ? ? ? ?return goods_bean; ? ? ? ?} ? ? ? ?return null; ? ?}}
點(diǎn)擊事件出不來
qq_Linjj藍(lán)極光輝_04362138
2017-12-15 10:05:08