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

发布于 2024-09-16 04:47:37 字数 2273 浏览 2 评论 0原文

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

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

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}

谢谢您的帮助。

I'm attempting to loop through database values creating tabs. I have setup a method called createTab which accepts a Long value and a String value. It's working with static data, but I'm struggling to understand how to loop through SQLite database records.

Here is my failed attempt (replace [lessthan] with less than symbol):

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

You probably don't need the LogCat to know what I did wrong in the code above, but just in case...

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}

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追我者格杀勿论 2024-09-23 04:47:37

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

You need to position the cursor before you can call getLong or getString. After you issue a query, the Cursor is positioned just before the first row, so you should call moveToNext in your loop. Something like:

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"));
}
我ぃ本無心為│何有愛 2024-09-23 04:47:37

在我的代码中,我从 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
           }
     }

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

In my code I have get value from SQLite database and store that value in array of object of model Class (FriendData is my model class) using Cursor.

   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
           }
     }

First I have Check Condition for Cursor is it null and move Cursor Object for pointing to First record which are fetched. Then I have compute size of cursor using method getCount(). After that for loop is used to store all data in array of object and at the end cursor is pointing to next record which are fetched.

清晨说晚安 2024-09-23 04:47:37

使用以下 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")));    

}

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

Use the following for loop:

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

Although , while loop would be much simpler:

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

}

In both cases, you dont need to set the cursor's starting position manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文