微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 如何使用cursoradapter实现自动完成功能

我有一个sqlite数据库,包含2个表4000行,每行用于自动完成.我看到了非常简单的示例,它们使用字符串数组来提供自动完成功能,或者使用联系人列表来执行相同操作.显然,在我的案例中,这些都不起作用.对于自动完成,如何将自己的sqlite数据库与我自己的自动完成数据一起使用.我是否必须创建内容提供商?怎么样?请给我一些例子,因为我找不到任何例子.我设法覆盖sqliteOpenHelper将数据库从assets文件夹复制到android上的/ data / data / MY_PACKAGE / databases /文件夹.我创建了一个自定义CursorAdapter,它使用我的自定义sqliteOpenHelper并从runQueryOnBackgroundThread返回一个游标.关于某些_id列丢失,我得到了奇怪的错误.我已将_id列添加到表中.我也不明白Filterable接口在做什么以及何时过滤我的数据.我需要覆盖哪些方法/类?谢谢.

解决方法:

有用.

您需要从here开始的sqliteOpenHelper.您基本上必须将数据库复制到assets文件夹中的特定文件夹中.然后,您需要一个使用自定义sqliteOpenHelper的自定义CursorAdapter.

这是我的活动的onCreate方法.


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);

        KeywordsCursorAdapter kwadapter = new KeywordsCursorAdapter(this, null);

        txtKeyword = (AutoCompleteTextView)this.findViewById(R.id.txtKeyword);
        txtKeyword.setAdapter(kwadapter);
        txtCity = (AutoCompleteTextView)this.findViewById(R.id.txtCity);
        btnSearch = (Button)this.findViewById(R.id.btnSearch);
        btnSearch.setonClickListener(this);
    }

这是cursoradapter.您可以在构造时为游标传递null.


public class KeywordsCursorAdapter extends CursorAdapter {

    private Context context;

    public KeywordsCursorAdapter(Context context, Cursor c) {
        super(context, c);
        this.context = context;
    }

    //I store the autocomplete text view in a layout xml.
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.keyword_autocomplete, null);
        return v;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        String keyword = cursor.getString(cursor.getColumnIndex("keyword"));
        TextView tv = (TextView)view.findViewById(R.id.txtAutocomplete);
        tv.setText(keyword);
    }

    //you need to override this to return the string value when
    //selecting an item from the autocomplete suggestions
    //just do cursor.getstring(whatevercolumn);
    @Override
    public CharSequence convertToString(Cursor cursor) {
        //return super.convertToString(cursor);
        String value = "";
        switch (type) {
        case Keywords:
            value = cursor.getString(DatabaseHelper.KEYWORD_COLUMN);
            break;
        case Cities:
            value = cursor.getString(DatabaseHelper.CITY_COLUMN);
            break;
        }
        return value;
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        //return super.runQueryOnBackgroundThread(constraint);
        String filter = "";
        if (constraint == null) filter = "";
        else
            filter = constraint.toString();
                //I have 2 DB-s and the one I use depends on user preference
        SharedPreferences prefs  = PreferenceManager.getDefaultSharedPreferences(context);
        //String selectedCountryCode = prefs.getString("selectedCountry", "GB");
        String selectedCountryCode = prefs.getString(context.getString(R.string.settings_selected_country), "GB");
        selectedCountryCode += "";

                //Here i have a static sqliteOpenHelper instance that returns a cursor.
        Cursor cursor = MyApplication.getDbHelpers().get(selectedCountryCode.toLowerCase()).getKeywordsCursor(filter);
        return cursor;
    }
}

这是返回光标的部分:它只是一个具有相似条件的选择.


public class DatabaseHelper extends sqliteOpenHelper {

...

    public synchronized Cursor getKeywordsCursor (String prefix) {
        if (database == null) database = this.getReadableDatabase();
        String[] columns = {"_id", "keyword"};
        String[] args = {prefix};

        Cursor cursor;
        cursor = database.query("keywords", columns, "keyword like '' || ? || '%'", args, null, null, "keyword", "40");

        int idcol = cursor.getColumnIndexOrThrow("_id");
        int kwcol = cursor.getColumnIndexOrThrow("keyword");

        while(cursor.movetoNext()) {
            int id = cursor.getInt(idcol);
            String kw = cursor.getString(kwcol);
            Log.i("keyword", kw);
        }

        cursor.movetoPosition(-1);
        return cursor;
    }

...

}

您还可以创建自定义内容提供程序,但在这种情况下,它只是您需要覆盖的另一个无用的类.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐