开发者

How do I implement autocomplete with cursoradapter

I have an SQLite database containing 2 tables 4000+ rows each used for autocomplete. I saw very simple examples that use an array of strings to provide autocomplete or they use the list of contacts to do the same. Obviously none of these work in my case. How do I use my own SQLite database with my own autocomplete data, for the autocomplete. Do I have to create content providers? How? Please give me some examples because I couldn't find any. I have managed to override SQLiteOpenHelper to copy the database from the assets folder to the /data/data/MY_PACKAGE/databases/ folder on the android. I have created a custom CursorAdapter that uses my custom SQLiteOpenHelper and returns a cu开发者_如何学编程rsor from runQueryOnBackgroundThread. I get strange errors about some _id column missing. I have added the _id column to my tables. I also don't understand what is the Filterable interface doing and when does my data get filtered. What methods/classes do I need to override? Thanks.


It works.

You need the SQLiteOpenHelper from here. You basically have to copy your database into a specific folder from your assets folder. Then you need a custom CursorAdapter that uses your custom SQLiteOpenHelper.

Here is the onCreate method for my activity.


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

Here is the cursoradapter. You can pass null for cursor when constructing.


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

Here is the part that returns the cursor: it's just a select with a like condition.


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

...

}

You can also create a custom content provider but in this case it would be just another useless class you need to override.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜