如何通过sqlite android中搜索词的首字母获取所有数据?

How to get all data through starting letter of the searched word in sqlite android?

我有搜索字符串。例如:如果我键入 co,我需要获取键入的 c 的所有起始字母并显示列表。

但它只显示准确的 ca 列表。如何仅显示输入字符串中字母表的首字母。

在下面的查询中,显示文本是 table,它显示给定输入的描述。

public List<BaseTableOfContents> searchNodesForText(@Nullable String rootHash,
                                                            @Nullable String substring) {
        List<BaseTableOfContentsNode> returnValue = null;
        if (StringUtils.isNotEmpty(substring) && StringUtils.isNotEmpty(rootHash)) {
            String likeQuery = "%" + substring + "%";
            Cursor cursor = mDatabase.query(Tables.TOC_NODES, null,
                                            Columns.TOC_ROOT_NODE_HASH + " = ? AND (" +
                                            Columns.DISPLAY_TEXT + " LIKE ? OR " +
                                            Columns.TOC_IS_TITLE_BREAK + " = 1)",
                                            new String[] { rootHash, likeQuery }, null, null,
                                            Columns.TOC_SORT_ORDER + ORDER_ASCENDING);

            returnValue = getNodesFromCursor(cursor);
            cursor.close();
        }

下面是硬编码的。您可以看到硬编码的 "compensacion" 文本。但是如果我输入 compensacion,它应该列出所有以 "c"

开头的列表
public List<BaseTableOfContents> searchNodesForText(@Nullable String rootHash,
                                                            @Nullable String substring) {
        List<BaseTableOfContentsNode> returnValue = null;
        if (StringUtils.isNotEmpty(substring) && StringUtils.isNotEmpty(rootHash)) {
            String likeQuery = "%" + "compensacion" + "%"; //here I hardcoded. Only when I give exact term, it's displaying, otherwise it's not displaying.
            Cursor cursor = mDatabase.query(Tables.TOC_NODES, null,
                                            Columns.TOC_ROOT_NODE_HASH + " = ? AND (" +
                                            Columns.DISPLAY_TEXT + " LIKE ? OR " +
                                            Columns.TOC_IS_TITLE_BREAK + " = 1)",
                                            new String[] { rootHash, likeQuery }, null, null,
                                            Columns.TOC_SORT_ORDER + ORDER_ASCENDING);

            returnValue = getNodesFromCursor(cursor);
            cursor.close();
        }

使用这个

public List<BaseTableOfContents> searchNodesForText(@Nullable String rootHash,
                                                            @Nullable String substring) {
        List<BaseTableOfContentsNode> returnValue = null;
        if (StringUtils.isNotEmpty(substring) && StringUtils.isNotEmpty(rootHash)) {
            String likeQuery = "%" + substring.indexOf(0) + "%";
            Cursor cursor = mDatabase.query(Tables.TOC_NODES, null,
                                            Columns.TOC_ROOT_NODE_HASH + " = ? AND (" +
                                            Columns.DISPLAY_TEXT + " LIKE ? OR " +
                                            Columns.TOC_IS_TITLE_BREAK + " = 1)",
                                            new String[] { rootHash, likeQuery }, null, null,
                                            Columns.TOC_SORT_ORDER + ORDER_ASCENDING);

            returnValue = getNodesFromCursor(cursor);
            cursor.close();
        }

substring.indexOf(0) take the first character of your text and show all data that start with that letter.