I want to save my
ArrayList
in SQLite database and later retr
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
Tutorial Link
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.
How about you itterate through that arraylist and just add it to the database...?
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))
}
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) {
}
}