使用Loaders的CursorIndexOutOfBoundsException

时间:2015-06-26 14:22:23

标签: android android-cursorloader android-loadermanager android-loader

我得到了这个例外

  

06-26 15:49:43.390:E / AndroidRuntime(1337):android.database.CursorIndexOutOfBoundsException:索引-1请求,大小为1

这个游标检索2列,虽然我使用快速搜索框相同的数据库,它显示3列数据,问题是这个游标检索两列,虽然提供者下面从3列检索数据

  • 此代码片段,我从光标

    获取数据
     @Override
     public Loader<Cursor> onCreateLoader(int arg0, Bundle data) {
     Uri uri = SuggestProvider.CONTENT_URI;
     return new CursorLoader(getBaseContext(), uri,
            null, null , new String[]{data.getString("query")}, null);
     }
     @Override
     public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
     mCursorAdapter.swapCursor(c);
     int Id = c.getColumnIndex(database.FIELD_ID);
     int Title = c.getColumnIndex(database.SONG);
     int Artist = c.getColumnIndex(database.ARTIST);
    
     do {
        long thisId = c.getLong(Id);
        String thisTitle = c.getString(Title);
        String thisArtist = c.getString(Artist);
        songList.add(new Song(thisId, thisTitle, thisArtist));
      }
        while (c.moveToNext());
     }
    
  • 在此代码段提供程序中获取uri

     @Override
       public Cursor query(Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {
        Cursor c = null;
        switch (sURIMatcher.match(uri)) {
        case SEARCH_RECORDS:
            c = table.getSongs(selectionArgs);
            break;
        case GET_RECORD:
            c = table.getSongs(selectionArgs);
            break;
        case SEARCH_SUGGEST:
            String id = uri.getLastPathSegment();
            c = table.getSong(id);
        }
       return c;
      }
    
  • 函数getSongs数据库表,我得到数据

    public Cursor getSongs(String[] selectionArgs){
    
    String selection = SONG + " like ? ";
    
    if(selectionArgs!=null){
        selectionArgs[0] = "%"+selectionArgs[0] + "%";
    }
    
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setProjectionMap(mAliasMap);
    
    queryBuilder.setTables(TABLE_NAME);
    
    Cursor c = queryBuilder.query(helper.getReadableDatabase(),
            new String[] { "_ID",
                    SearchManager.SUGGEST_COLUMN_TEXT_1 ,
                    SearchManager.SUGGEST_COLUMN_TEXT_2 ,
                    SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID
            } ,
            selection,
            selectionArgs,
            null,
            null,
            SONG + " asc ","1"
    );
    return c;
    

    }

1 个答案:

答案 0 :(得分:0)

为什么CursorLoader没有选择的selectionArgs?你的意思是“查询”订购吗?

值得一试,有时候,在使用DB时,切换掉掉并重新创建表的数据库版本可以帮助..