android spf 存储 集合(实体等)
package com.example.sharedpreferencelist;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;
import android.util.Base64;
public class Utils {
public static String SceneList2String(ArrayList SceneList) {
System.out.println("SceneList 长度///:"+SceneList.size());
// 实例化一个ByteArrayOutputStream对象,用来装载压缩后的字节文件。
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = null;
try {
// 然后将得到的字符数据装载到ObjectOutputStream
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
} catch (IOException e) {
System.out.println("字符数据装载到ObjectOutputStream 出错。。。");
e.printStackTrace();
}
try {
// writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它
objectOutputStream.writeObject(SceneList);
} catch (IOException e) {
System.out.println("writeObject 写入特定类的对象的状态出错。。。");
e.printStackTrace();
}
// 最后,用Base64.encode将字节文件转换成Base64编码保存在String中
String SceneListString = new String(Base64.encode(
byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
try {
objectOutputStream.close();// 关闭objectOutputStream
} catch (IOException e) {
e.printStackTrace();
}
return SceneListString;
}
@SuppressWarnings("unchecked")
public static List String2SceneList(String SceneListString) {
byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),
Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
mobileBytes);
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(byteArrayInputStream);
} catch (StreamCorruptedException e) {
System.out
.println("objectInputStream StreamCorruptedException异常 出错。。。");
e.printStackTrace();
} catch (IOException e) {
System.out.println("objectInputStream IOException异常 出错。。。");
e.printStackTrace();
}
List SceneList = null;
try {
SceneList = (List) objectInputStream.readObject();
} catch (OptionalDataException e) {
System.out.println("SceneList OptionalDataException异常 出错。。。");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("SceneList ClassNotFoundException异常 出错。。。");
e.printStackTrace();
} catch (IOException e) {
System.out.println("SceneList IOException异常 出错。。。");
e.printStackTrace();
}
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return SceneList;
}
}
//下面是具体的用法
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//储存
ArrayList<String> list = new ArrayList<String>();
list.add("1111");
list.add("2222");
list.add("3333");
list.add("4444");
SharedPreferences mySharedPreferences = getSharedPreferences("userlist", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = mySharedPreferences.edit();
String liststr = Utils.SceneList2String(list);
edit.putString("userinfo", liststr);
edit.commit();
}
});
//得到
findViewById(R.id.get).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("userlist", Context.MODE_PRIVATE);
String liststr = sharedPreferences.getString("userinfo", "");
ArrayList<String> showSceneList = (ArrayList) Utils.String2SceneList(liststr);
for (String a : showSceneList) {
Log.i("数据",a);
}
}
});