获取 SQLite 数据库并将其存储在对象数组中

发布于 2024-11-25 19:41:29 字数 319 浏览 1 评论 0原文

我正在查看 Android SDK 中的 Notes 应用程序示例。我想学习如何做,而不是使用 CursorAdapter 传递给 ListAdpater/ListView 进行排序,我想知道如何自己处理数据;特别是在 ArrayList 形式中。

在该示例中,注释基本上具有 id、标题和正文。我想知道如何查询 SQLite 数据库并使用它返回的游标来收集数据并创建我将调用 Note 的对象实例,该对象具有 id、title 和 body 参数。我最终想将所有这些对象存储到一个ArrayList中进行管理。我只是不确定如何处理游标。

这是一个非常普遍的问题,但我只需要有人指出我正确的方向。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别再吹冷风 2024-12-02 19:41:29

我可能不明白你的问题,但你需要查询你的数据库然后将游标数据添加到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();

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();
明月夜 2024-12-02 19:41:29

其实我自己玩了之后就明白了。所以我意识到使用 Cursor.getColumnIndex("name_of_column") 将返回列的索引,以便在诸如 cursor.getInt(cursor.getColumnIndex("_id")); 这样的命令中使用代码>.我所要做的就是使用 for 循环遍历整个列表,并使用 cursor.moveToNext() 迭代收集的行。我在发布这个问题后几分钟就想到了这个。 :)

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

计㈡愣 2024-12-02 19:41:29

这可能对你有帮助,

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文