开发者

Getting SQLite database and storing it in an array of objects

I'm looking at the Notes app example from the Android SDK. What I want to learn how to do is instead of using a CursorAdapter to just pass to a ListAdpater/ListView to sort out, I want to know how I can deal with the data myself; particularly in an ArrayList form.

In the example, a note basically has an id, title, and body. I want to know how I can query the SQLite database and use the cursor it returns to collect the data and create object instances of an object I'll call Note which has parameters for id, title, and body. I ultimately want to store all these objects into an ArrayList for management. 开发者_如何学JAVAI'm just not sure how to deal with a Cursor.

This is a pretty general question but I just need someone to point me in the right direction.


I might not get your question but you need to query your db then add cursor data to ArrayList?

    List<String> pointsList = new ArrayList<String>();
    database = openOrCreateDatabase("favorites", SQLiteDatabase.OPEN_READWRITE, null);
    if(database!=null)
    {
        c= database.rawQuery(QUERY, null);
        c.moveToFirst();
        while(c.isAfterLast()==false)
        {
            pointsList.add(c.getInt(c.getColumnIndex("id")); // do the same for other columns
            c.moveToNext();
        }

    }
   database.close();
   c.close();


Actually I figured it out myself after playing around. So I realized that using cursor.getColumnIndex("name_of_column") will return the column's index to be used in commands like cursor.getInt(cursor.getColumnIndex("_id"));. All I have to do is just use a for loop to go through the whole list and use cursor.moveToNext() to just iterate through the rows collected. I came up with this minutes after I posted this question. :)


This might help you,

   public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
    {
        // create an ArrayList that will hold all the data collected from
        // the database.
        ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();

        // This is a database call that creates a "cursor" object.
        // The cursor object store the information collected from the
        // database and is used to iterate through the data.
        Cursor cursor;

        try
        {
            // ask the database object to create the cursor.
            cursor = db.query(
                    TABLE_NAME,
                    new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
                    null, null, null, null, null
            );

            // move the cursors pointer to position zero.
            cursor.moveToFirst();

            // if there is data after the current cursor position, add it
            // to the ArrayList.
            if (!cursor.isAfterLast())
            {
                do
                {
                    ArrayList<Object> dataList = new ArrayList<Object>();

                    dataList.add(cursor.getLong(0));
                    dataList.add(cursor.getString(1));
                    dataList.add(cursor.getString(2));

                    dataArrays.add(dataList);
                }
                // move the cursor's pointer up one position.
                while (cursor.moveToNext());
            }
        }
        catch (SQLException e)
        {
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }

        // return the ArrayList that holds the data collected from
        // the database.
        return dataArrays;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜