如何在游标中使用子字符串?

时间:2014-12-04 09:18:21

标签: android android-listview android-sqlite android-cursor android-query

我有Cursor Select from sqlitequery我做了substring但我可以substring使用它{&#t}} 39; t告诉我任何错误,但显示listView为空。

    try {
        String value = editText.getText().toString();
        cursor = sql.rawQuery(
                "SELECT MetaDataID,Data,CategoryID,ParentID FROM Book WHERE Data LIKE '"
                        + "%" + value + "%'", null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    Struct_Search note = new Struct_Search();
                    note.MetaData = cursor.getInt(cursor.getColumnIndex("MetaDataID"));
                    Result = cursor.getString(cursor.getColumnIndex("Data"));
                    int A = Result.indexOf(value);
                    String V = Result.substring(A,100);
                    note.Value = V;
                    note.NumberAyeh = cursor.getInt(cursor.getColumnIndex("CategoryID"));
                    ParentID = cursor.getInt(cursor.getColumnIndex("ParentID"));
                    CursorSecond = sql.rawQuery("SELECT name FROM ContentList WHERE id ="+ ParentID, null);
                    if (CursorSecond != null) {
                        do {
                            CursorSecond.moveToFirst();
                            note.NameSureh = CursorSecond.getString(CursorSecond.getColumnIndex("name"));
                            CursorSecond.close();
                        } while (CursorSecond.moveToNext());
                    }
                    notes.add(note);
                } while (cursor.moveToNext());
            }
            adapter.notifyDataSetChanged();
        }
    } catch (Exception e) {
    } finally {
        cursor.close();
    }

注意:此行不起作用:

                String V = Result.substring(A,100);
                note.Value = V;

1 个答案:

答案 0 :(得分:1)

如果您想在字符索引后获取第一个100个字符,并将note.Value设置为它,请将您的子字符串更改为

 // substring(int beginIndex, int endIndex)
 Result.substring(A, A + 100);

在java中,substring的第二个参数是endIndex,而不是Length。