I want to save my
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>()
in SQLite database and later retrieve it to display the items stored in it. How can I achieve this?
Please provide examples.
Thanks in Advance :)
Asked
Active
Viewed 9,124 times
1

Divya Jain
- 393
- 1
- 6
- 22

Jaydroid
- 334
- 3
- 13
-
@MikeM. Thanks ... but i don't know, in which way will this article help me?? – Jaydroid Mar 19 '14 at 05:10
-
Sorry, I thought your question was how to implement an SQLite database. My bad. – Mike M. Mar 19 '14 at 05:15
-
http://mrbool.com/how-to-insert-data-into-a-sqlite-database-in-android/28895 – Dhaval Mar 19 '14 at 05:31
5 Answers
0
You can serialize it using the default standard java serializer or us your own lib. Following your perf. needs: you can read this benchmark: https://github.com/eishay/jvm-serializers/wiki

Fred
- 21
- 3
-
Thanks @Fred ... i guess this might work ... if there are any other simple ways, please let me know :) – Jaydroid Mar 19 '14 at 05:23
0
Is there more than one records in your HashMap. If so, You will save every records. There are best way save to file as a serialized object Save to file:
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();
}
}
}
Read from file:
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) {
}
}

beka
- 11
- 3
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))
}

Android
- 8,995
- 9
- 67
- 108
-1
How about you itterate through that arraylist and just add it to the database...?

JoxTraex
- 13,423
- 6
- 32
- 45
-2
Please refer to the link above. I has a working example of using HashMap for SqLite operations using SQliteOpenHelper. It has one insertion at a time, but you might wanna create a loop, if you wish to insert them all in one instance.

Dhaval
- 1,597
- 11
- 15
-
yeah i've gone through this , will implement it to my requirements and let u know .... thanks :) – Jaydroid Mar 19 '14 at 06:15