1

我正在尝试遍历创建选项卡的数据库值。我设置了一个名为 createTab 的方法,它接受 Long 值和 String 值。它正在处理静态数据,但我很难理解如何遍历 SQLite 数据库记录。

这是我失败的尝试(将 [lessthan] 替换为小于符号):


for (int i = 0; i [lessthan] mCursor.getCount(); i++)
{
 createTab(
  mCursor.getLong(mCursor.getColumnIndexOrThrow("_id")),
  mCursor.getString(mCursor.getColumnIndexOrThrow("category")));
}

您可能不需要 LogCat 就知道我在上面的代码中做错了什么,但以防万一……

08-27 21:28:18.268: 错误/AndroidRuntime(232): 由: android.database.CursorIndexOutOfBoundsException: 请求索引 -1,大小为 3
08-27 21:28:18.268: 错误/AndroidRuntime(232): 在 android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
08-27 21:28:18.268: 错误/AndroidRuntime(232): 在 android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172)
08-27 21:28:18.268: 错误/AndroidRuntime(232): 在 android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:99)
08-27 21:28:18.268: 错误/AndroidRuntime(232): 在 com.toBuy.Main.createTabs(Main.java:51)
08-27 21:28:18.268: 错误/AndroidRuntime(232): 在 com.toBuy.Main.onCreate(Main.java:38)
08-27 21:28:18.268: 错误/AndroidRuntime(232): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-27 21:28:18.268: 错误/AndroidRuntime(232): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
08-27 21:28:18.268: 错误/AndroidRuntime(232): ... 11 更多
08-27 21:28:18.278: INFO/Process(54): 发送信号。PID:232 SIG:3
08-27 21:28:18.278: INFO/dalvikvm(232): threadid=7: 对信号 3 做出反应
08-27 21:28:18.338: INFO/dalvikvm(232): 将堆栈跟踪写入“/data/anr/traces.txt”
08-27 21:28:18.508: INFO/ARMAssembler(54): 生成扫描线__00000077:03515104_00000000_00000000 [27 ipp] (41 ins) at [0x285a68:0x285b0c] in 713498 ns
08-27 21:28:18.518: INFO/ARMAssembler(54): 生成扫描线__00000077:03515104_00001001_00000000 [64 ipp] (84 ins) at [0x285b10:0x285c60] in 1897448 ns
08-27 21:28:28.086: WARN/ActivityManager(54): 启动超时已过期,放弃唤醒锁!
08-27 21:28:28.097: WARN/ActivityManager(54): HistoryRecord{4390bf70 com.toBuy/.Main} 的活动空闲超时

谢谢您的帮助。

4

3 回答 3

1

您需要先定位光标,然后才能调用getLonggetString。发出查询后,Cursor位于第一行之前,因此您应该moveToNext在循环中调用。就像是:

int size = mCursor.getCount();
for (int i = 0; i < size; i++) {

    // Position the cursor
    mCursor.moveToNext();

    // Fetch your data values
    long id = mCursor.getLong(mCursor.getColumnIndex("_id"));
    String cat = mCursor.getString(mCursor.getColumnIndex("category"));
}
于 2010-08-27T21:59:25.270 回答
1

在我的代码中,我从 SQLite 数据库中获取值,并使用光标将该值存储在模型类的对象数组中(FriendData 是我的模型类)。

   Cursor cursor = friendAdapter.fetchData(); //call method for fetch data from databse and fetched data is stored into cursor
    int cursorSize = cursor.getCount();

    if (cursor != null && cursor.moveToFirst()) {
         FriendData[] friendData=new FriendData[cursorSize];  //create array of objects as size of cursor

            for (int i = 0; i < cursorSize; i++) {
                   friendData[i]=new FriendData();

                  /*store fetched data in some variables. In my code I have store value of cursor in friendName String variable*/
                  friendName = cursor.getString(cursor.getColumnIndex(FriendAdapter.FRIEND_NM));

                 /* set friendName variable value in array of object*/
                 friendData[i].setFriendName(friendName);

              cursor.moveToNext();  //move cursor for pointing to next fetched record
           }
     }

首先,我检查光标的条件是否为空并移动光标对象以指向获取的第一条记录。然后我使用 getCount() 方法计算光标的大小。之后,for循环用于将所有数据存储在对象数组中,最后光标指向获取的下一条记录。

于 2017-02-09T07:33:31.647 回答
0

使用以下 for 循环:

   for(mCursor.moveToFirst();!mCursor.isAfterLast();mCursor.moveToNext()){
        createTab(
  mCursor.getLong(mCursor.getColumnIndexOrThrow("_id")),
  mCursor.getString(mCursor.getColumnIndexOrThrow("category")));    
        }

虽然,while 循环会简单得多:

while (mCursor.moveToNext()) { createTab(
  mCursor.getLong(mCursor.getColumnIndexOrThrow("_id")),
  mCursor.getString(mCursor.getColumnIndexOrThrow("category")));    

}

在这两种情况下,您都不需要手动设置光标的起始位置。

于 2013-04-25T09:56:33.230 回答