Getting SQLite database and storing it in an array of objects

南笙酒味 提交于 2019-11-29 22:36:24

问题


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. I'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.


回答1:


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();



回答2:


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. :)




回答3:


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;
    }


来源:https://stackoverflow.com/questions/6781708/getting-sqlite-database-and-storing-it-in-an-array-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!