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

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

问题描述

我看从Android SDK中的注意事项应用的例子。我想学习如何做的,而不是使用一个CursorAdapter的只是传递给ListAdpater / ListView控件进行梳理,我想知道我怎么能与自己的数据处理;特别是在一个ArrayList的形式。在该示例中,注释基本上具有一个id,标题和正文。我想知道我怎么可以查询SQLite数据库,并使用它返回来收集数据,并创建一个对象,我会打电话请注意这对ID,标题和正文参数对象实例的指针。我最终想所有这些对象存储到一个ArrayList进行管理。我只是不知道如何处理一个游标。这是一个pretty的普遍问题,但我只是需要有人点我在正确的方向。

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)); 。我所要做的只是用一个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天全站免登陆