Star
Star

Reputation: 707

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

I have search string. For eg: If I type co I need to get all the starting letter of the typed c and display the list.

But it's displaying only the exact ca list. How to display only the starting letter of the alphabet from the input string.

in the below query, Display Text is the table which displays the description from the given input.

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

Below is hardcoded one. You can see "compensacion" text which is hardcoded. But if i type compensacion, it should list down all the list starts with "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();
        }

Upvotes: 0

Views: 204

Answers (1)

sushildlh
sushildlh

Reputation: 9056

use this

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.

Upvotes: 1

Related Questions