获取 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能不明白你的问题,但你需要查询你的数据库然后将游标数据添加到ArrayList?
I might not get your question but you need to query your db then add cursor data to ArrayList?
其实我自己玩了之后就明白了。所以我意识到使用 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 likecursor.getInt(cursor.getColumnIndex("_id"));
. All I have to do is just use a for loop to go through the whole list and usecursor.moveToNext()
to just iterate through the rows collected. I came up with this minutes after I posted this question. :)这可能对你有帮助,
This might help you,