我有这个函数,用于查找名称中包含子字符串的行。但我得到错误,虽然使用 log.v 我可以看到结果返回正常。谁能告诉我这里有什么问题?
这是我的 findAlbum 函数:
public ArrayList<Albums> findAlbum(String keyword) {
ArrayList<Albums> albums = new ArrayList<Albums>();
Cursor c = database.rawQuery("SELECT * FROM favorites " + "WHERE "
+ ALB_NAME + " LIKE '%" + keyword + "%';", null);
c.moveToFirst();
if (c != null) {
while (!c.isAfterLast()) {
for (int i = 0; i < c.getColumnCount(); i++) {
String id = c.getString(c.getColumnIndex("id"));
String name = c.getString(c.getColumnIndex(ALB_NAME));
String singer = c.getString(c.getColumnIndex(ALB_SINGER));
String genre = c.getString(c.getColumnIndex(ALB_GENRE));
String mix = c.getString(c.getColumnIndex(ALB_MIX));
String thumb = c.getString(c.getColumnIndex(ALB_THUMB));
albums.add(new Albums(id, name, singer, genre, mix, thumb));
Log.v("--", "FOUND FROM DB:" + albums.get(i).getTAG_NAME());
c.moveToNext();
}
}
}
return albums;
}
和 LogCat 输出:
04-04 08:30:45.138: E/AndroidRuntime(7773): android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
04-04 08:30:45.138: E/AndroidRuntime(7773): at com.myapp.functions.DatabaseHelper.findAlbum(DatabaseHelper.java:75)
04-04 08:30:45.138: E/AndroidRuntime(7773): at com.myapp.activities.Favorites$1.afterTextChanged(Favorites.java:67)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.widget.TextView.sendAfterTextChanged(TextView.java:7655)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8057)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.text.SpannableStringBuilder.sendTextHasChanged(SpannableStringBuilder.java:900)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:353)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:663)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:188)
04-04 08:30:45.138: E/AndroidRuntime(7773): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:149)
04-04 08:30:45.138: E/AndroidRuntime(7773): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
04-04 08:30:45.138: E/AndroidRuntime(7773): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.os.Looper.loop(Looper.java:137)
04-04 08:30:45.138: E/AndroidRuntime(7773): at android.app.ActivityThread.main(ActivityThread.java:4440)
04-04 08:30:45.138: E/AndroidRuntime(7773): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 08:30:45.138: E/AndroidRuntime(7773): at java.lang.reflect.Method.invoke(Method.java:511)
04-04 08:30:45.138: E/AndroidRuntime(7773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-04 08:30:45.138: E/AndroidRuntime(7773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-04 08:30:45.138: E/AndroidRuntime(7773): at dalvik.system.NativeStart.main(Native Method)