2014-03-19 93 views
0

我想將我的 ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>()保存在SQLite數據庫中,稍後檢索它以顯示存儲在其中的項目。我怎樣才能做到這一點? 請提供示例。 在此先感謝:)如何將我的Arraylist保存到SQLite數據庫中?

+0

@MikeM。謝謝......但我不知道,這篇文章會以哪種方式幫助我? – Jaydroid

+0

對不起,我以爲你的問題是如何實現一個SQLite數據庫。我的錯。 –

+0

沒問題@MikeM。乾杯:) – Jaydroid

回答

-2

Tutorial Link

請參考上面的鏈接。我有一個使用SQliteOpenHelper的SqLite操作使用HashMap的工作示例。它一次只有一個插入,但如果您希望將其全部插入到一個實例中,您可能需要創建一個循環。

+0

是的,我已經通過這個,將它實現我的要求,讓你知道....謝謝:) – Jaydroid

+0

這個例子不能正常工作... PLZ查 – Jaydroid

0

您可以使用默認的標準java序列化程序或我們自己的lib序列化它。 繼你的表演。需要:你可以閱讀這個基準測試:https://github.com/eishay/jvm-serializers/wiki

+0

謝謝@Fred ...我想這可能會工作...如果有是任何其他簡單的方法,請讓我知道:) – Jaydroid

-1

你如何通過數組列表迭代並將其添加到數據庫中?

0

HashMap中是否有多個記錄。如果是這樣,您將保存每條記錄。 有最好的方式保存到文件作爲序列化對象 保存到文件:

FileOutputStream fos = null; 
    ObjectOutputStream oos = null; 
    try { 
     List<Map<String, String> list=new ArrayList<HashMap<String, String>>(); 

     fos = getContext().openFileOutput(OFFERS_FILE_NAME, 
       Context.MODE_PRIVATE); 
     oos = new ObjectOutputStream(fos); 
     oos.writeObject(list); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (fos != null) { 
       fos.flush(); 
       fos.close(); 
      } 
      if (oos != null) { 
       oos.flush(); 
       oos.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

從文件中讀取:

List<Map<Integer, String>> orderData = null; 

FileInputStream fis = null; 
ObjectInputStream ois = null; 
try { 
fis = getContext().openFileInput(OFFERS_FILE_NAME); 
ois = new ObjectInputStream(fis); 
orderData = (HashMap<Integer, String>) ois.readObject(); 
} catch (Exception e) { 
e.printStackTrace(); // We have not data in SD Card 

} finally { 
     try { 
     if (fis != null) 
      fis.close(); 
     if (ois != null) 
      ois.close(); 
     } catch (Exception e) { 
} 
     } 
0
for (int i = 0; i < yourlistname.size(); i++) 
        { 
          String sql = "INSERT or replace INTO tbl_Resources (resourceID, contentID, imgpath) " 

              + "VALUES ('" 
              + sitesList.get(i) 
              + "', '" 
              + sitesList.get(i) 
              + "', '" 
//           
              +sitesList.get(i) 
              + "')";     
         db.execSQL(sql); 

        } 


for (int i = 0; i < list.size(); i++) { 

      //insert stat like by this you will get listCountry.get(i)) 

     } 
相關問題