i get this Exception
06-26 15:49:43.390: E/AndroidRuntime(1337): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
This cursor retrieve 2 columns although i am use the same database with Quick Search box and it display data from 3 columns ,the problem is this cursor retrieve two columns although provider below retrieve Data from 3 columns
this code snippet where i get data from cursor
@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()); }
In this snippet provider get 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; }
function getSongs Database table where i get Data
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;
}
Why the CursorLoader has selectionArgs without selection ? did you mean to order it by "query"?
Worth a try ,sometimes ,when working with DB's ,switching the DB version which drop and recreate the table can help..