Java 如何使用cursoradapter实现自动完成

Java 如何使用cursoradapter实现自动完成,java,android,sqlite,autocomplete,android-cursoradapter,Java,Android,Sqlite,Autocomplete,Android Cursoradapter,我有一个SQLite数据库,其中包含两个表4000+行,每个行用于自动完成。我看到了一些非常简单的例子,它们使用字符串数组来提供自动完成功能,或者使用联系人列表来实现自动完成功能。显然,在我的情况下,这些都不起作用。如何使用我自己的SQLite数据库和我自己的自动完成数据进行自动完成。我必须创建内容提供商吗?怎么用?请给我举一些例子,因为我找不到。我已成功覆盖SQLiteOpenHelper,将数据库从资产文件夹复制到android上的/data/data/MY_PACKAGE/database

我有一个SQLite数据库,其中包含两个表4000+行,每个行用于自动完成。我看到了一些非常简单的例子,它们使用字符串数组来提供自动完成功能,或者使用联系人列表来实现自动完成功能。显然,在我的情况下,这些都不起作用。如何使用我自己的SQLite数据库和我自己的自动完成数据进行自动完成。我必须创建内容提供商吗?怎么用?请给我举一些例子,因为我找不到。我已成功覆盖
SQLiteOpenHelper
,将数据库从资产文件夹复制到android上的/data/data/MY_PACKAGE/databases/文件夹。我已经创建了一个自定义
CursorAdapter
,它使用我的自定义
SQLiteOpenHelper
并从
runQueryOnBackgroundThread
返回一个光标。我发现了一些奇怪的错误,一些id列丢失了。我已将_id列添加到我的表中。我也不明白可过滤接口在做什么,我的数据什么时候被过滤。我需要重写哪些方法/类?谢谢。

它很有效

您需要来自的SQLiteOpenHelper。基本上,您必须将数据库从资产文件夹复制到特定文件夹中。然后,您需要一个使用自定义SQLiteOpenHelper的自定义游标适配器

下面是我的活动的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);
    }
这是游标适配器。构造时,可以为游标传递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;
    }
}
下面是返回光标的部分:它只是一个具有类似条件的select


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;
    }

...

}

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

该选择条件的含义是什么?
select _id,关键字来自关键字,其中的关键字类似于“| |?| |”%”<代码>?
是参数<代码>|是串联。我不确定如何将参数转换为字符串,这就是我使用空字符串连接的原因。它可能应该是
?| |'%'我有一个类似的问题,但我就是无法修复它:|。。。游标=数据库。查询(“关键字”,列,“关键字如“”||?||“%””,参数,null,null,“关键字”,“40”);。。。。。现在,第一个“关键字”是表的名称?但仍然不明白你所说的“关键字,比如“| |”?| |“%”是什么意思。。。。“关键字”是一条无法替换的消息吗????“?”是参数…还是不明白。如果你能再进一步解释一下,我将不胜感激,还有一件事。。。。。public Cursor runQueryOnBackgroundThread(CharSequence constraint)返回一个新的游标,但它不过滤自动完成的条目…您必须设置一个过滤器,并且您没有执行与u代码中类似的任何操作:)下面是一个类似sql的语法示例:<代码>?
只是一个参数,它将在运行时被sqlite替换为有效字符串。我认为,对于每个按下的字符,都会执行runQueryOnBackgroundThread,并返回一个新的游标。是的,我可能应该重用光标,但我很高兴我终于让它工作了,我没有优化它。