我如何使用游标和Sqlite DB来从游标上的最后一个条目移动到第一个条目(反之亦然)呢?

时间:2022-01-31 08:10:49

I have a Sqlite DB that I'm accessing with a Cursor. I can scroll through the cursor and run my "check();" method. the issue I'm having is when I scroll past the cursor.

我有一个用光标访问的Sqlite DB。我可以滚动游标并运行我的“check()”方法。我遇到的问题是当我滚动光标时。

ie: hitting my previous button when on the first entry or hitting my next button when on the last entry...

在第一个条目按上一个按钮,或者在最后一个条目按下下一个按钮……

I tried to put in an if statement to automatically move to the proper location.

我试图输入if语句来自动移动到正确的位置。

What I'm trying to do is if I'm on the first position and hit the previous button I want to move to the last position in the cursor and continue.

我要做的是,如果我在第一个位置,点击上一个按钮,我想移动到光标的最后一个位置并继续。

Also if the cursor is in the last position and tries to scroll past that I'd like it to go to the first position. Below is my attempt at trying to make this happen.

同样,如果光标在最后一个位置,并试图滚动过去,我希望它到第一个位置。下面是我的尝试。

@Override public void onClick(View v) {

@Override public void onClick(View v) {

    View src = v;
    switch (src.getId()) {
    case R.id.ButtonPrevious:       

        if  ((main.cursor.moveToPrevious()) ==  (main.cursor.moveToFirst()))
        {
            main.cursor.moveToLast();

        }else{
            main.cursor.moveToPrevious();   
        }                               
        check();



    case R.id.ButtonNext:


        if  ((main.cursor.moveToNext()) == (main.cursor.moveToLast()))
        {
            main.cursor.moveToFirst();

        }else{
        main.cursor.moveToNext();
        }
        check();
    }
}

1 个解决方案

#1


3  

You need to utilize the isAfterLast and isBeforeFirst methods of the Cursor class. Do something like this

您需要使用游标类的isAfterLast和isBeforeFirst方法。这样做

case R.id.ButtonPrevious:
    cursor.moveToPrevious();
    if (cursor.isBeforeFirst()){
        cursor.moveToLast();
    }
    check();
case R.id.ButtonNext:
    cursor.moveToNext();
    if (cursor.isAfterLast()){
        cursor.moveToFirst();
    }
    check();

Not all the code, but the critical part. Hope that helps.

不是所有的代码,而是关键的部分。希望有帮助。

#1


3  

You need to utilize the isAfterLast and isBeforeFirst methods of the Cursor class. Do something like this

您需要使用游标类的isAfterLast和isBeforeFirst方法。这样做

case R.id.ButtonPrevious:
    cursor.moveToPrevious();
    if (cursor.isBeforeFirst()){
        cursor.moveToLast();
    }
    check();
case R.id.ButtonNext:
    cursor.moveToNext();
    if (cursor.isAfterLast()){
        cursor.moveToFirst();
    }
    check();

Not all the code, but the critical part. Hope that helps.

不是所有的代码,而是关键的部分。希望有帮助。