android 保存ArrayList至SharedPreferences,
分享于 点击 21849 次 点评:50
android 保存ArrayList至SharedPreferences,
今天,简单讲讲如何保持ArrayList<Object>到SharedPreferences。
之前讲了保持ArrayList<String>到SharedPreferences的内容,但是如果集合里是类的话,怎么保持到SharedPreferences呢?
在网上找到两种代码。
一.将list转为json进行保存。
1 首先将自定义对象序列化
public class CoordinateAlterSample implements Serializable {
private double x;
private double y;
private String name;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
其次,将list转为json,即可保存到SharedPreferences中
List<CoordinateAlterSample> alterSamples = new ArrayList<CoordinateAlterSample>();
SharedPreferences.Editor editor = getSharedPreferences("AlterSamplesList", MODE_PRIVATE).edit();
Gson gson = new Gson();
String json = gson.toJson(alterSamples);
Log.d(TAG, "saved json is "+ json);
editor.putString("alterSampleJson", json);
editor.commit();
对应的取出操作为:
SharedPreferences preferences = getSharedPreferences("AlterSamplesList", MODE_PRIVATE);
String json = preferences.getString("alterSampleJson", null);
if (json != null)
{
Gson gson = new Gson();
Type type = new TypeToken<List<CoordinateAlterSample>>(){}.getType();
List<CoordinateAlterSample> alterSamples = new ArrayList<CoordinateAlterSample>();
alterSamples = gson.fromJson(json, type);
for(int i = 0; i < alterSamples.size(); i++)
{
Log.d(TAG, alterSamples.get(i).getName()+":" + alterSamples.get(i).getX() + "," + alterSamples.get(i).getY());
}
}
这里用到了Gson 的jar包,可以自己下载一下。
二.将数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在 XML文件中
需要用到:commons-codec-1.4.jar
AddNewWord addWord=new AddNewWord();
addWord.setWords(word.getWords());
addWord.setWordClass(i);
SharedPreferences mySharedPreferences=getSharedPreferences("new_word", Activity.MODE_WORLD_READABLE);
ByteArrayOutputStream baos = new ByteArrayOutputStream(3000);
ObjectOutputStream oos=null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(addWord);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 将Product对象放到OutputStream中
// 将Product对象转换成byte数组,并将其进行base64编码
String newWord = new String(Base64.encodeBase64(baos.toByteArray()));
SharedPreferences.Editor editor = mySharedPreferences.edit();
// 将编码后的字符串写到base64.xml文件中
editor.putString("newWord", newWord);
editor.commit();
读取对象:
String wordBase64 = mySharedPreferences.getString("new_word", "");
// 对Base64格式的字符串进行解码
byte[] base64Bytes = Base64.decodeBase64(wordBase64 .getBytes());
ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
// 从ObjectInputStream中读取Product对象
AddNewWord addWord= (AddNewWord ) ois.readObject();
对象实体:
public class AddNewWord implements Serializable{
private static final long serialVersionUID = -37782648386953312L;
private String words;
private int wordClass;
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
public int getWordClass() {
return wordClass;
}
public void setWordClass(int wordClass) {
this.wordClass = wordClass;
}
@Override
public String toString() {
return "AddNewWord [words=" + words
+ ", wordClass=" + wordClass
+ "]";
}
}
这里的做法和List<String>一样,将list的数据按照一定的顺序存放,然后按照相同的顺序读取。具体可以看我写的关于存贮list<String>的博客。
android 保存ArrayList<Object>至SharedPreferences就讲完了。
就这么简单。
相关文章
- 暂无相关文章
用户点评