获取 SQLite 数据库并将其存储在对象数组中 [英] Getting SQLite database and storing it in an array of objects

查看:43
本文介绍了获取 SQLite 数据库并将其存储在对象数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看来自 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.

推荐答案

其实我自己玩了一会就想通了.所以我意识到使用 cursor.getColumnIndex("name_of_column") 将返回列的索引,用于像 cursor.getInt(cursor.getColumnIndex("_id"));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. :)

这篇关于获取 SQLite 数据库并将其存储在对象数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆