Android - Load Values From Sqlite Database To An Arraylist
I am new to android . I have an application working with SQLite DB. I need to push values from database to an arraylist of type object. The code i used is given here. private Arr
Solution 1:
try this:
private ArrayList<objectname> objectList = getResults();
private ArrayList<objectname> getResults() {
MyDbdb=newMyDb(this); //my database helper file
db.open();
ArrayList<objectname> resultList = newArrayList<objectname>();
Cursorc= db.getAllValues(); //function to retrieve all values from a table- written in MyDb.java filewhile (c.moveToNext())
{
Stringdate= c.getString(c.getColumnIndex("date"));
try
{
ob = newobjectname();
ob.setDate(date);// setDate function is written in Class file
resultList.add(ob);
}
catch (Exception e) {
Log.e(MY_DEBUG_TAG, "Error " + e.toString());
}
}
c.close();
db.close();
return resultList;
}
Solution 2:
I had a similar issue with retrieving values from the db using cursors and displaying on screen.. The below solution works for me..
Cursor cur = db.getAllValues();
int index = c.getColumnIndex("date");
cur.moveToFirst();
while (cur.isAfterLast() == false) {
System.out.println(cur.getString(index)); // For debug purposes
String date = cur.getString(index);
try {
ob = new objectname();
ob.setDate(date);
resultList.add(ob);
} catch (Exception e) {
Log.e(MY_DEBUG_TAG, "Error " + e.toString());
}
cur.moveToNext();
}
cur.close();
Post a Comment for "Android - Load Values From Sqlite Database To An Arraylist"