购物车数据存储器是贯穿整个App的,在商品详情页、商品列表页,只要有加入购物车按钮,都要将商品加入上存储器中,然后在购物车页面的RecyclerView中显示出来。下面,就来实现购物车数据存储器:
①定义商品Bean
public class Wares implements Serializable {
private Long id;
private String name;
private String imgUrl;
private String description;
private Float price;
}
②定义加入购物车内的商品Bean
public class ShoppingCart extends Wares implements Serializable {
private int count;
private boolean isChecked=true;
}
这两个Bean都是可以序列化的,所以可以跨进程传递。
③将Bean转化为json数据,来存储到SharedPreference中,实现数据存储器的功能。所以首先要有两个工具类来操作SharedPreference和解析json数据。
public class PreferencesUtils {
public static String PREFERENCE_NAME = "Cniao_Pref_Common";
public static boolean putString(Context context, String key, String value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
return editor.commit();
}
public static String getString(Context context, String key) {
return getString(context, key, null);
}
public static String getString(Context context, String key, String defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getString(key, defaultValue);
}
public static boolean putInt(Context context, String key, int value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(key, value);
return editor.commit();
}
public static int getInt(Context context, String key) {
return getInt(context, key, -1);
}
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getInt(key, defaultValue);
}
public static boolean putLong(Context context, String key, long value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putLong(key, value);
return editor.commit();
}
public static long getLong(Context context, String key) {
return getLong(context, key, -1);
}
public static long getLong(Context context, String key, long defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getLong(key, defaultValue);
}
public static boolean putFloat(Context context, String key, float value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putFloat(key, value);
return editor.commit();
}
public static float getFloat(Context context, String key) {
return getFloat(context, key, -1);
}
public static float getFloat(Context context, String key, float defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getFloat(key, defaultValue);
}
public static boolean putBoolean(Context context, String key, boolean value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, value);
return editor.commit();
}
public static boolean getBoolean(Context context, String key) {
return getBoolean(context, key, false);
}
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getBoolean(key, defaultValue);
}
}
public class JSONUtil {
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);
}
}
④实现数据存储器:
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();
}
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;
}
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 put(Wares wares){
ShoppingCart cart = convertData(wares);
put(cart);
}
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;
}
public ShoppingCart convertData(Wares item){
ShoppingCart cart = new ShoppingCart();
cart.setId(item.getId());
cart.setDescription(item.getDescription());
cart.setImgUrl(item.getImgUrl());
cart.setName(item.getName());
cart.setPrice(item.getPrice());
return cart;
}
}
现在,一个可持久化存储的购物车数据存储器就实现了,