android序列化與反序列話HashMap到sqlite數據庫

1.首先給出序列化與反序列化工具類

[html]
public class SerializableInterface { 
    public SerializableInterface(){ 
 
    } 
 
    public static byte[] serialize(HashMap<String, String> hashMap){ 
        try { 
        ByteArrayOutputStream mem_out = new ByteArrayOutputStream(); 
            ObjectOutputStream out = new ObjectOutputStream(mem_out); 
 
            out.writeObject(hashMap); 
 
            out.close(); 
           mem_out.close(); 
 
           byte[] bytes =  mem_out.toByteArray(); 
           return bytes; 
        } catch (IOException e) { 
            return null; 
        } 
    } 
 
     public static HashMap<String, String> deserialize(byte[] bytes){ 
        try { 
            ByteArrayInputStream mem_in = new ByteArrayInputStream(bytes); 
            ObjectInputStream in = new ObjectInputStream(mem_in); 
 
            HashMap<String, String> hashMap = (HashMap<String, String>)in.readObject(); 
 
             in.close(); 
             mem_in.close(); 
 
             return hashMap; 
        } catch (StreamCorruptedException e) { 
            return null; 
        } catch (ClassNotFoundException e) { 
            return null; 
        }   catch (IOException e) { 
            return null; 
        } 
     } 

2.定義一個方法獲取學生的信息,其中傢庭成員信息保存在hashmap後,序列化到sqlite

[html]
public ArrayList<ContentValues> formatStudentInfo() { 
        ArrayList<ContentValues> valueArr = null; 
        ContentValues values = null; 
        HashMap<String, String> map = null; 
 
        valueArr = new ArrayList<ContentValues>();    
        //第一組數據 www.aiwalls.com  
        values = new ContentValues(3); 
                                     
        values.put("name", "david"); 
        values.put("class","0502"); 
        map = new HashMap<String, String>(); 
        map.put("mom", "lucy"); 
        map.put("dad", "jack"); 
        byte[] bytes = SerializableInterface.serialize(map); 
        values.put("family", bytes); 
        valueArr.add(values); 
         
        //第二組數據 
        values = new ContentValues(3); 
         
        values.put("name", "joy"); 
        values.put("class","0502"); 
        map = new HashMap<String, String>(); 
        map.put("mom", "mary"); 
        map.put("dad", "json"); 
        bytes = SerializableInterface.serialize(map); 
        values.put("family", bytes); 
        valueArr.add(values); 
          
        return valueArr; 
    } 
3.調用數據庫的操作類將
[html]
formatStudentInfo返回的數據insert到數據庫。 
這個方法應該放在自己寫的繼承瞭SQLiteOpenHelper的類中 
    public void insertAll(String databaseName, 
            ArrayList<ContentValues> valuesArr) { 
        SQLiteDatabase db = getWritableDatabase(); 
        db.beginTransaction();  
 
        for (ContentValues val : valuesArr) { 
            db.insert(databaseName, null, val); 
        } 
 
        db.setTransactionSuccessful();  
        db.endTransaction(); 
        db.close(); 
    } 
 
讀取的時候調用 
Cursor cursor db.query("fix your self"); 
……. 
//通過getBlob方法獲取bytes後反序列化得到map對象,參數x是序列化字段所在的列數(從0開始計數)。 
byte[] bytes = cursor.getBlob(x); 
HashMap<String, String> map = SerializableInterface.deserialize(bytes); 

以上僅是對序列化與反序列化HashMap對象做一個簡單的介紹,代碼並不完整,數據庫操作部分需要自己補充完整。

 

摘自 weidawei0609的專欄

發佈留言