gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:40:18 25 4
gpt4 key购买 nike

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

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

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

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.

关于java - 如何在sqlite android中通过搜索单词的首字母获取所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52965052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com