2

我有这个函数,用于查找名称中包含子字符串的行。但我得到错误,虽然使用 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)
4

2 回答 2

4

您的问题是您试图在索引 2 处获取项目,但 Cursor 的大小为 2 并且索引从 0 开始,因此您试图在不存在的索引处获取项目。

我认为你真的不需要你的内部 for 循环,这个循环是你的问题。你在那里打电话

c.moveToNext();

与列数一样多。因此,例如,如果您有五列并且 Cursor 有两行,则您的 moveToNext() 方法将被调用 5 次,这将导致CursorIndexOutOfBoundsException。所以删除内部循环,它应该可以工作。

我建议您将代码更改为以下内容:

if (c != null)
   if (c.moveToFirst()) {
      do {         
         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());
      } while (c.moveToNext());
   }
}
于 2013-04-04T06:41:39.800 回答
1

您应该使用c.getCount()而不是c.getColumnCount(),因为您正在遍历行,而不是列。

于 2013-04-04T06:41:27.680 回答