android autocompletetextview should show only relevant options in drop down

核能气质少年 提交于 2019-12-06 01:54:37

You have to tell the adapter what items to display. I tried implementing something similar to this by using a FilterQueryProvider that queries the database for the items that I want to display in the dropdown.

FilterQueryProvider filter = new FilterQueryProvider() {

    @Override
    public Cursor runQuery(CharSequence constraint) {
        // Make a DB query that filters based on the constraint

        return //whatever query results;
    }
};
myAdapter.setFilterQueryProvider(filter);

As for the situation when you select an item on the list, you have to override the CursorToStringConverter of the SimpleCursorAdapter. Something like:

SimpleCursorAdapter.CursorToStringConverter conv = new SimpleCursorAdapter.CursorToStringConverter() {

    @Override
    public CharSequence convertToString(Cursor cursor) {
        int numCol = cursor.getColumnIndexOrThrow("whateverFieldYouNeed");
        String term = cursor.getString(numCol);
        return term;
    }
};
myAdapter.setCursorToStringConverter(conv);

Instead of the CursorToStringConverter you could also use

mAdapter.setStringConversionColumn(mCursor.getColumnIndexOrThrow("whateverFieldYouNeed"));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!