Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ArrayList<objectname> objectList  = new ArrayList<objectname>();
 //function used to get values from database
   public ArrayList<objectname> getResults() {

    MyDb db = new MyDb(this); //my database helper file
    db.open();
    Cursor c = db.getAllValues(); //function to retrieve all values from a table- written in MyDb.java file
    if (c.moveToFirst())
    {
        do {         
            String date = c.getString(c.getColumnIndex("date"));

            try
            {
                ob = new objectname();
                ob.setDate(c.getString(c.getColumnIndex("date")));// setDate function is written in Class file
                objectList.add(ob);
            }
            catch (Exception e) {
             Log.e(MY_DEBUG_TAG, "Error " + e.toString());
            }

         } while (c.moveToNext());
    }
       db.close();
       return objectList;
}

This is not working correctly. Not shown any errors but i didn't get values in to the arraylist objectList

Please help me..Thanks in advance..

like image 621
Aswini K Avatar asked Mar 30 '11 05:03

Aswini K


People also ask

How can I retrieve single data from SQLite database in Android?

We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data. This method return the total number of columns of the table.

How fetch data from SQLite database and show in listview?

xml. Create an another layout file (list_row. xml) in /res/layout folder to show the data in listview, for that right click on layout folder à add new Layout resource file à Give name as list_row. xml and write the code like as shown below.

Is SQLite good for Android?

Android SQLite is a very lightweight database which comes with Android OS. Android SQLite combines a clean SQL interface with a very small memory footprint and decent speed. For Android, SQLite is “baked into” the Android runtime, so every Android application can create its own SQLite databases.


2 Answers

try this:

private ArrayList<objectname> objectList  = getResults();

private ArrayList<objectname> getResults() {

    MyDb db = new MyDb(this); //my database helper file
    db.open(); 

    ArrayList<objectname> resultList = new ArrayList<objectname>();


    Cursor c = db.getAllValues(); //function to retrieve all values from a table- written in MyDb.java file
    while (c.moveToNext())
    {
        String date = c.getString(c.getColumnIndex("date"));

        try
        {
            ob = new objectname();
            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;
}
like image 65
runor49 Avatar answered Oct 05 '22 17:10

runor49


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();
like image 31
venus Avatar answered Oct 05 '22 19:10

venus