【问题标题】:Two Cursors, ListView, CursorIndexOutOfBoundsException两个游标,ListView,CursorIndexOutOfBoundsException
【发布时间】:2011-09-02 04:00:17
【问题描述】:

作为 android java 世界的初学者,我需要你的帮助。我遇到了著名的“CursorIndexOutOfBoundsException”问题。

我正在使用 SQLite db,并且我有两个游标,我正在从数据库中获取一些行。 我需要使用第二个光标 (c2) 在某些条件下获取一些先前的值,并将这些值放在 ListView 上。

代码有一个例外: “android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为 0”。

如果我忽略此异常但我想修复它,ListView 看起来不错。

我知道它与 Cursor 相关联。我试图检查一些条件 - 它没有帮助。也许如果您可以查看我的代码,您会发现原因在哪里。

代码:

public void LoadLogGrid()
{
    dbHelper=new DatabaseHelper(this);
    try
    {

    int LogName = (int) spinLog.getSelectedItemId();
    Cursor c=dbHelper.getLogByLogID(LogName);
    if (c != null) c.moveToFirst();
    int count = c.getCount();


    if (c.moveToFirst()){
        ArrayList<String> mArrayList = new ArrayList<String>();

    int i=0;
    do {

        int sVar1 = c.getInt(c.getColumnIndex("Var1")); 
        Long sId = (long) c.getInt(c.getColumnIndex("_id"));            

        Cursor c2=dbHelper.getPrevLogByLogID(LogName,sVar1);    
        c2.moveToFirst();

        if (c2!=null) {
            String sPrevOdo = c2.getString(c2.getColumnIndex("Odo"));
                mArrayList.add(sPrevOdo);
               c2.close();
        } else {
              //stopManagingCursor(c2); 
              //c2.close();
            Log.d("A:", "Something");
        }


        String [] from=new String []{"Date","Col1","Col2","Col3"};
        int [] to=new int [] {R.id.logDate,R.id.logCol1,R.id.logCol2,R.id.logCol3,R.id.rowOpt2};
        SimpleCursorAdapter sca=new LogCursorAdapter(this,R.layout.loggridrow,c,from,to,mArrayList);
        grid.setAdapter(sca);   
        registerForContextMenu(grid);
        i++;    

    } while (c.moveToNext());

        c.close();

    dbHelper.close();
    }   

    }
    catch(Exception ex)
    {
        AlertDialog.Builder b=new AlertDialog.Builder(this);
        b.setMessage(ex.toString());
        b.show();
    }

}

在第二个游标中查询:

public Cursor getPrevLogByLogID(long LogID, long Var1)
     {
         SQLiteDatabase db=this.getReadableDatabase();
        String[] params=new String[]{String.valueOf(LogID),String.valueOf(Var1)};

         Cursor c2=db.rawQuery("SELECT LogID as _id, Col1 from Log WHERE Col2=? AND Col3<? AND Full=1 ORDER BY Odo DESC", params);
         if (c2 != null) { c2.moveToFirst();}
         return c2;
     }  

【问题讨论】:

    标签: android exception listview sqlite cursor


    【解决方案1】:

    尝试改变你的

    do {
        ....
    } while (c.moveToNext());
    

    while (c.moveToNext()) {
        ....
    }
    

    现在的方式,无论如何它都会至少运行一次循环。

    【讨论】:

    • 谢谢,但没用。更改此设置后,listView 出现更多错误。目前,除了“异常”之外,一切看起来都像过去一样。@Karthik 是的,但是该怎么做。也许我做错了,我想我尝试了这个解决方案(可能我做错了什么)
    【解决方案2】:

    您已将 c2 移动到 c2.moveToFirst() 的位置,然后您正在检查它是否为空,我认为光标应该为空,以便引发异常尝试进行检查将光标移动到第一个位置之前的条件

    【讨论】:

    • 是的,但如何做到这一点。也许我做错了,我想我尝试了这个解决方案(可能我做错了什么)
    • 如果 (c2!=null) { String sPrevOdo = c2.getString(c2.getColumnIndex("Odo")); mArrayList.add(sPrevOdo); c2.close();在您的情况下条件为真并且 mArrayList 被填充?
    猜你喜欢
    • 2019-09-15
    • 2023-03-03
    • 2012-08-21
    • 2013-04-03
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多