android spf 存儲 集合(實體等)

package com.example.sharedpreferencelist;java

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;android

import android.util.Base64;ide

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) {code

e.printStackTrace();
}
return SceneList;
}對象

}get

 

 

//下面是具體的用法it

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);        }    }});
相關文章
相關標籤/搜索