android开发之SharedPreferences存储对象

因为作者最近在做一个收藏的功能,涉及到了数据的存储和读取,又因为太懒,不想去使用ContentProvider,所以种种原因让我选择了使用SharedPreferences来存储数据,但是存储一条、两条单独的数据是可以的,但是要存储对象!作者只能说咋搞!怕啥,一步一步的来嘛!

第一步:在csdn网站,下载“commons-codec-1.4.jar”

第二步:

public class Obj2SharedUtils {
    public static void saveFavorites_Entity(Context context,Favorites_Entity entity,String id) {
        SharedPreferences preferences = context.getSharedPreferences("myfavorites",context.MODE_PRIVATE);
        // 创建字节输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // 创建对象输出流,并封装字节流
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            // 将对象写入字节流
            oos.writeObject(entity);
            // 将字节流编码成base64的字符窜
            String favorites_Base64 = new String(Base64.encodeBase64(baos.toByteArray()));
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(id, favorites_Base64);
            editor.commit();
        } catch (IOException e) {
        }
        Log.e("ok", "存储成功");
    }

    public static  Favorites_Entity readFavorites_Entity(Context context,String id) {
        Favorites_Entity entity = null;
        SharedPreferences preferences = context.getSharedPreferences("myfavorites",context.MODE_PRIVATE);
        String favoritesBase64 = preferences.getString(id, "");

        //读取字节
        byte[] base64 = Base64.decodeBase64(favoritesBase64.getBytes());

        //封装到字节流
        ByteArrayInputStream bais = new ByteArrayInputStream(base64);
        try {
            //再次封装
            ObjectInputStream bis = new ObjectInputStream(bais);
            try {
                //读取对象
                entity = (Favorites_Entity) bis.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return entity;
    }
}

第三步:

infos_favorites = (ImageView)findViewById(R.id.infos_favorites);
infos_favorites.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View v) {
		f_Entity = new Favorites_Entity();
		if(sp.getString(id,"0").equals(id)) {
			sEditor.remove(id);
			sEditor.commit();
			editor.putString(id,"0");
			editor.commit();
			infos_favorites.setImageResource(R.drawable.icon7);
			Toast.makeText(News_Infos_Activity.this,"取消收藏",Toast.LENGTH_SHORT).show();
			return;
		}
		Log.e("收藏id",id);
		editor.putString(id,id);
		editor.commit();
		infos_favorites.setImageResource(R.drawable.icon8);
		f_Entity.setFavorites_Id(id);
		f_Entity.setFavorites_Url(url);
		f_Entity.setFirst_typeid(entity.getFirst_typeid());
		f_Entity.setFavorites_Title(entity.getNews_title());
		sd = sdf.format(new Date());
		f_Entity.setFavorites_Time(sd);
		//将对象保存到SharedPreferences中
		Obj2SharedUtils.saveFavorites_Entity(News_Infos_Activity.this,f_Entity,id);
		Toast.makeText(News_Infos_Activity.this,"收藏成功",Toast.LENGTH_SHORT).show();
	}
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值