androidandroid-listviewandroid-sqliteandroid-cursorandroid-query

How I can use substring in cursor?


I have a Cursor that Select from sqlite, in this query I do a substring but I can'y use from substring it don't get me any error but show me listView empty.

    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();
    }

Notice : This line don't work :

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

Solution

  • If you are trying to get the FIRST 100 characters AFTER index of character, and set note.Value to it, change your substring to be

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

    In java, the second parameter of substring is endIndex, not Length.