18.显示购物车存储器实现(实际上就是存储在sharedPrefrence当中)

来源:互联网 发布:德国布线火遍网络 编辑:程序博客网 时间:2024/05/17 04:56

购物车里面的数据实现本地化,可以使用shardeprdfrence来存储数据,因为sharedprefrence不能存储List<类>数据,所以使用Gson,将数据解析成json,然后在存储在sharedprefrence中。

步骤:1.编写购物车的javabean。。。ShoppingCart:

package zuo.com.ui.util;import android.content.Context;import android.util.SparseArray;import com.google.gson.reflect.TypeToken;import java.util.ArrayList;import java.util.List;import zuo.com.ui.bean.ShoppingCart;/**映日sharedprefrence中不能存储对象数据,所以将购物页面中的对象保存成json字符串,然后保存在sharedprefrence中,用的时候在解析读取 * Created by <a href="http://www.cniao5.com">菜鸟窝</a> * 一个专业的Android开发在线教育平台 */public class CartProvider {    public static final String CART_JSON="cart_json";    private SparseArray<ShoppingCart> datas =null;    private  Context mContext;    public CartProvider(Context context){        mContext = context;       datas = new SparseArray<>(10);        listToSparse();    }    public void put(ShoppingCart cart){       ShoppingCart temp =  datas.get(cart.getId().intValue());        if(temp !=null){            temp.setCount(temp.getCount()+1);        }        else{            temp = cart;            temp.setCount(1);        }        datas.put(cart.getId().intValue(),temp);        commit();    }    public void update(ShoppingCart cart){        datas.put(cart.getId().intValue(),cart);        commit();    }    public void delete(ShoppingCart cart){        datas.delete(cart.getId().intValue());        commit();    }    public List<ShoppingCart> getAll(){        return  getDataFromLocal();    }    public void commit(){        List<ShoppingCart> carts = sparseToList();        PreferencesUtils.putString(mContext,CART_JSON,JSONUtil.toJSON(carts));    }    private List<ShoppingCart> sparseToList(){        int size = datas.size();        List<ShoppingCart> list = new ArrayList<>(size);        for (int i=0;i<size;i++){            list.add(datas.valueAt(i));        }        return list;    }    private void listToSparse(){        List<ShoppingCart> carts =  getDataFromLocal();        if(carts!=null && carts.size()>0){            for (ShoppingCart cart:                    carts) {                datas.put(cart.getId().intValue(),cart);            }        }    }    public  List<ShoppingCart> getDataFromLocal(){        String json = PreferencesUtils.getString(mContext,CART_JSON);        List<ShoppingCart> carts =null;        if(json !=null ){            carts = JSONUtil.fromJson(json,new TypeToken<List<ShoppingCart>>(){}.getType());        }        return  carts;    }}

2.编写工具类:JSONUtil,PrefrencesUtils:

/**JSONUtil.java*Created on 2014-9-29 上午9:54 by Ivan*Copyright(c)2014 Guangzhou Onion Information Technology Co., Ltd.*http://www.cniao5.com*/package zuo.com.ui.util;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import java.lang.reflect.Type;/** * Created by Ivan on 14-9-29. * Copyright(c)2014 Guangzhou Onion Information Technology Co., Ltd. * http://www.cniao5.com */public class JSONUtil {   //创建json对象    private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();    public static Gson getGson(){        return  gson;    }    public static <T> T fromJson(String json,Class<T> clz){        return  gson.fromJson(json,clz);    }    public static <T> T fromJson(String json,Type type){        return  gson.fromJson(json,type);    }    public static String toJSON(Object object){       return gson.toJson(object);    }}

package zuo.com.ui.bean;/**购物车页面的javabean,存储在shareprefrence中 * Created by Administrator on 2016/10/17. */public class ShoppingCart {    private int count;    private boolean isChecked=true;    private Long id;    private String name;    private String imgUrl;    private String description;    private Float price;    public ShoppingCart(int count, boolean isChecked, Long id, String name, String imgUrl, String description, Float price) {        this.count = count;        this.isChecked = isChecked;        this.id = id;        this.name = name;        this.imgUrl = imgUrl;        this.description = description;        this.price = price;    }    public int getCount() {        return count;    }    public void setCount(int count) {        this.count = count;    }    public boolean isChecked() {        return isChecked;    }    public void setChecked(boolean checked) {        isChecked = checked;    }    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getImgUrl() {        return imgUrl;    }    public void setImgUrl(String imgUrl) {        this.imgUrl = imgUrl;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }    public Float getPrice() {        return price;    }    public void setPrice(Float price) {        this.price = price;    }}

3.最后边写购物车存储数据的方法方法CartProvider

package zuo.com.ui.util;import android.content.Context;import android.util.SparseArray;import com.google.gson.reflect.TypeToken;import java.util.ArrayList;import java.util.List;import zuo.com.ui.bean.ShoppingCart;/**映日sharedprefrence中不能存储对象数据,所以将购物页面中的对象保存成json字符串,然后保存在sharedprefrence中,用的时候在解析读取 * Created by <a href="http://www.cniao5.com">菜鸟窝</a> * 一个专业的Android开发在线教育平台 */public class CartProvider {    public static final String CART_JSON="cart_json";    private SparseArray<ShoppingCart> datas =null;    private  Context mContext;    public CartProvider(Context context){        mContext = context;       datas = new SparseArray<>(10);        listToSparse();    }    public void put(ShoppingCart cart){       ShoppingCart temp =  datas.get(cart.getId().intValue());        if(temp !=null){            temp.setCount(temp.getCount()+1);        }        else{            temp = cart;            temp.setCount(1);        }        datas.put(cart.getId().intValue(),temp);        commit();    }    public void update(ShoppingCart cart){        datas.put(cart.getId().intValue(),cart);        commit();    }    public void delete(ShoppingCart cart){        datas.delete(cart.getId().intValue());        commit();    }    public List<ShoppingCart> getAll(){        return  getDataFromLocal();    }    public void commit(){        List<ShoppingCart> carts = sparseToList();        PreferencesUtils.putString(mContext,CART_JSON,JSONUtil.toJSON(carts));    }    private List<ShoppingCart> sparseToList(){        int size = datas.size();        List<ShoppingCart> list = new ArrayList<>(size);        for (int i=0;i<size;i++){            list.add(datas.valueAt(i));        }        return list;    }    private void listToSparse(){        List<ShoppingCart> carts =  getDataFromLocal();        if(carts!=null && carts.size()>0){            for (ShoppingCart cart:                    carts) {                datas.put(cart.getId().intValue(),cart);            }        }    }    public  List<ShoppingCart> getDataFromLocal(){        String json = PreferencesUtils.getString(mContext,CART_JSON);        List<ShoppingCart> carts =null;        if(json !=null ){            carts = JSONUtil.fromJson(json,new TypeToken<List<ShoppingCart>>(){}.getType());        }        return  carts;    }}



************

最重要的一点,要根据购物车中的数据,来设置相应的javabean

0 1
原创粉丝点击