gpt4 book ai didi

java - 使用SimpleCursorAdapter使用多个SQL列填充单个TextView(搜索框建议)

转载 作者:行者123 更新时间:2023-12-01 17:35:52 41 4
gpt4 key购买 nike

我正在使用SQL作为数据源。我的应用程序的搜索框具有自动提示功能,但目前只能返回1列(例如column1)的结果。我希望它能够在2列中搜索(因此,column1 + column2),并在自动提示下拉列表中显示两列的结果。这是我尝试实现该方法的代码,但是没有成功。

public void LoadSuggestions(){

final String[] from = new String[] {"column1", "column2"};
final int[] to = new int[] {R.id.suggestion_text}; //suggestion_text is the TextView to populate

suggestionAdapter = new SimpleCursorAdapter(MainActivity.this,
R.layout.suggestion_row,null, from, to,0){
@Override
public void changeCursor(Cursor cursor) {
super.swapCursor(cursor);
}
};

search.setSuggestionsAdapter(suggestionAdapter);

search.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionClick(int position) {

CursorAdapter ca = search.getSuggestionsAdapter();
Cursor cursor = ca.getCursor();
cursor.moveToPosition(position);
String clicked_word = cursor.getString(cursor.getColumnIndex("column1"));
search.setQuery(clicked_word, false);

search.clearFocus();
search.setFocusable(false);

Intent intent = new Intent(MainActivity.this, WordMeaningActivity.class);
Bundle bundle = new Bundle();
bundle.putString(("column1",clicked_word);
intent.putExtras(bundle);
startActivity(intent);

return true;
}

@Override
public boolean onSuggestionSelect(int position) {
return true;
}
});

search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
String text = search.getQuery().toString();

Pattern p = Pattern.compile("[A-Za-züÜöÖőŐűŰáÁéÉíÍóÓúÚ \\-.]{1,25}");
Matcher m = p.matcher(text);

if(m.matches())
{
Cursor c = myDbHelper.getMeaning(text);

if(c.getCount()==0)
{
showAlertDialog();
}
else
{
search.clearFocus();
search.setFocusable(false);

Intent intent = new Intent(MainActivity.this, WordMeaningActivity.class);
Bundle bundle = new Bundle();
bundle.putString(("column1", text);
intent.putExtras(bundle);
startActivity(intent);

}
}
else
{
showAlertDialog();
}

return false;
}

@Override
public boolean onQueryTextChange(final String s) {

search.setIconifiedByDefault(false);

Pattern p = Pattern.compile("[A-Za-züÜöÖőŐűŰáÁéÉíÍóÓúÚ \\-.]{1,25}");
Matcher m = p.matcher(s);

if(m.matches()) {
Cursor cursorSuggestion = myDbHelper.getSuggestions(s);
suggestionAdapter.changeCursor(cursorSuggestion);
}

return false;
}
});}


这是它背后的SQL查询

public Cursor getSuggestions(String text) {
Cursor c= myDatabase.rawQuery("SELECT _id, column1, column2 FROM words WHERE column1 LIKE '"+text+"%' AND column2 LIKE '"+text+"%' LIMIT 40", null);
return c;}


我认为主要问题在这里:

final String[] from = new String[] {"column1", "column2"};


因为使用此顺序,如果我搜索某些内容,它将返回column1的列表,但是如果我将顺序从{“ column1”,“ column2”}更改为{“ column2”,“ column1”},则自动提示将仅显示column2的结果。我了解我应该在此行中执行以下操作:

final int[] to = new int[] {R.id.suggestion_text};


但是我没有找到任何详细的信息,只是一个解决方案,其中列将显示在不同的行中,但是我需要两个结果都显示在一个下拉列表中。感谢您的时间和阅读本文!

最佳答案

好的,休息了几天后,我看清楚了,完成此操作的方法是更改​​此字符串:

Cursor c= myDatabase.rawQuery("SELECT _id, column1, column2 FROM words WHERE column1 LIKE '"+text+"%' AND column2 LIKE '"+text+"%' LIMIT 40", null);


对此:

Cursor c= myDatabase.rawQuery("SELECT _id, column1 FROM words WHERE column1 LIKE '"+text+"%' UNION SELECT _id, column2 FROM words WHERE column2 LIKE '"+text+"%' LIMIT 40", null);


这样,搜索将在两行中进行,因此将从两行中获取并显示建议。

关于java - 使用SimpleCursorAdapter使用多个SQL列填充单个TextView(搜索框建议),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61043074/

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