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();
	}
});

你可能感兴趣的:(@android开发经验,android应用,android开发,收藏功能)