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

Populate single TextView with multiple SQL columns using SimpleCursorAdapter (Suggestion for searchbox)

我正在使用 SQL 作为数据源。我的应用程序的搜索框具有自动提示功能,但目前它只能 return 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"};

因为使用此顺序,如果我搜索某些内容,它将 return 列 1 的列表,但如果我将顺序从 {"column1"、"column2"} 更改为 {"column2", "column1"}, autosuggestion 将只显示 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);

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