android studio数据存储

储存bean

1.添加
代码:

/**
                 * 储存
                 */
                Gson gson=new Gson();
                SharedPreferences sharedPreferences=getSharedPreferences(ConstantsUtils.SP_WISH_NAME,MODE_PRIVATE);//初始化储存
                SharedPreferences.Editor edit = sharedPreferences.edit();//编辑器
                String spStringJson = sharedPreferences.getString(ConstantsUtils.SP_WISH_KEY_LIST_BEAN_NAME, "");//取值,设置默认值
                if (TextUtils.isEmpty(spStringJson)) {
                    WishBean wishBean=new WishBean();//创建bean对象
                    //设置值
                    wishBean.setName(name);
                    wishBean.setAuthor(author);
                    wishBean.setSetWishEndTime(endtime);
                    wishBean.setBackground(Integer.parseInt(bg));
                    wishBean.setWishType(Integer.parseInt(type));
                    listBean.add(wishBean);
                    Log.d("WishSettingActivity","hello ,i love you "+listBean.toString());
                    //储存起来

                    String toJson = gson.toJson(listBean);//BeanList转JSon
                    edit.putString(ConstantsUtils.SP_WISH_KEY_LIST_BEAN_NAME,toJson);
//                wishBeanList.
                    edit.commit();//提交
                    Log.d("TAG", "onViewClicked: 为空");
                }else {
                    Log.d("TAG", "onViewClicked: 不为空");
                    List<WishBean> listWishBean=gson.fromJson(spStringJson,new TypeToken<List<WishBean>>(){}.getType());
//                    String s = listWishBean.toString();

//                    加入值
                    WishBean wishBean2=new WishBean();
                    wishBean2.setName(name);
                    wishBean2.setAuthor(author);
                    wishBean2.setSetWishEndTime(endtime);
                    wishBean2.setWishType(Integer.parseInt(type));
                    wishBean2.setBackground(Integer.parseInt(bg));
                    listWishBean.add(wishBean2);//加到listWishBean
                    //转换
                    String toJson2 = gson.toJson(listWishBean);
                    //储存
                    edit.putString(ConstantsUtils.SP_WISH_KEY_LIST_BEAN_NAME,toJson2);
                    //提交
                    edit.commit();
                    Log.d("TAG", "onViewClicked: "+listWishBean.toString());
                }

思路
sharePreferences只能储存基本类型,不能储存bean
Gson储存转为json

gson.toJson(listWishBean);

再提交

edit.commit();//记得提交

添加
判断是否存在数据,如果存在就添加
取出,转List

List<WishBean> listWishBean=gson.fromJson(spStringJson,new TypeToken<List<WishBean>>(){}.getType());

添加bean

WishBean wishBean2=new WishBean();
                    wishBean2.setName(name);
                    wishBean2.setAuthor(author);
                    wishBean2.setSetWishEndTime(endtime);
                    wishBean2.setWishType(Integer.parseInt(type));
                    wishBean2.setBackground(Integer.parseInt(bg));
                    listWishBean.add(wishBean2);//加到listWishBean

储存与提交

 //储存
                    edit.putString(ConstantsUtils.SP_WISH_KEY_LIST_BEAN_NAME,toJson2);
                    //提交
                    edit.commit();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值