如何在for循环或while循环中引用游标项?

时间:2010-08-27 21:31:07

标签: android cursor android-cursor

我正在尝试循环创建标签的数据库值。我已经设置了一个名为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: ERROR/AndroidRuntime(232): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:99)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at com.toBuy.Main.createTabs(Main.java:51)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at com.toBuy.Main.onCreate(Main.java:38)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     ... 11 more
08-27 21:28:18.278: INFO/Process(54): Sending signal. PID: 232 SIG: 3
08-27 21:28:18.278: INFO/dalvikvm(232): threadid=7: reacting to signal 3
08-27 21:28:18.338: INFO/dalvikvm(232): Wrote stack trace to '/data/anr/traces.txt'
08-27 21:28:18.508: INFO/ARMAssembler(54): generated scanline__00000077:03515104_00000000_00000000 [ 27 ipp] (41 ins) at [0x285a68:0x285b0c] in 713498 ns
08-27 21:28:18.518: INFO/ARMAssembler(54): generated scanline__00000077:03515104_00001001_00000000 [ 64 ipp] (84 ins) at [0x285b10:0x285c60] in 1897448 ns
08-27 21:28:28.086: WARN/ActivityManager(54): Launch timeout has expired, giving up wake lock!
08-27 21:28:28.097: WARN/ActivityManager(54): Activity idle timeout for HistoryRecord{4390bf70 com.toBuy/.Main}

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

在我的代码中,我从SQLite数据库中获取值,并使用Cursor将该值存储在模型类的对象数组中(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
           }
     }

首先,我有Cursor的Check Condition是null并移动Cursor Object以指向获取的First记录。然后我使用方法getCount()计算游标的大小。之后for循环用于存储对象数组中的所有数据,最后光标指向下一个被提取的记录。

答案 1 :(得分:0)

您需要先定位光标,然后才能拨打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"));
}

答案 2 :(得分: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")));    

}

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