android序列化與反序列話HashMap到sqlite資料庫

來源:互聯網
上載者:User

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.2cto.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的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.