android.database.CursorIndexOutOfBoundsException:请求索引1,大小为1

时间:2013-07-29 23:03:25

标签: android cursor

//这不起作用。它显示第一个记录并在此之后停止。我想要一个即使在最后一条记录之后也能继续运行的循环。我明白了光标从位置0开始     //代码

cur=db.getData(position);

    switch(v.getId())
    {

    case R.id.next :
    { 

        if (cur != null && cur.getCount()> 0 && position < cur.getCount() && position != cur.getCount()){
            cur.moveToPosition(position);
            textView1.setText(""+cur.getString(1));// Display Columns 
            position++;
            cur.moveToNext();
        }
}

//我想要一个循环来显示记录号

下一个按钮 例如:

1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 

后退按钮 例如:

5 4 3 2 1 5 4 3 2 1

随机按钮 例如:

3 4 5 1
5 1 2 3
4 5 1 2 

3 个答案:

答案 0 :(得分:5)

出现该异常,因为您试图将对象放在不存在的位置。记住索引从0开始。

直接来自DOCS:

 public abstract boolean moveToPosition (int position)

在API级别1中添加

将光标移动到绝对位置。值的有效范围是-1&lt; = position&lt; = count。

如果请求目标是可访问的,则此方法将返回true,否则返回false。 参数 定位从零开始的位置以移动到。 返回

whether the requested move fully succeeded. 

答案 1 :(得分:0)

if (cur != null && cur.getCount()> 0 && position < cur.getCount() && position != cur.getCount()){
                        cur.moveToPosition(position);
                        textView1.setText(""+cur.getString(1));// Display Columns 
                    }

答案 2 :(得分:0)

在增加位置之前调用get data,因为你试图在一个未存在的我的数据库中加入一个元素。

cur =db. getData(position) ;

然后

position++;

请记住首先声明position = 0