4

オートコンプリートに使用される4000行以上の2つのテーブルを含むSQLiteデータベースがあります。文字列の配列を使用してオートコンプリートを提供する、または連絡先のリストを使用して同じことを行う非常に単純な例を見ました。明らかに、私の場合、これらのどれも機能しません。オートコンプリート用に、独自のオートコンプリートデータを使用して独自のSQLiteデータベースを使用するにはどうすればよいですか。コンテンツプロバイダーを作成する必要がありますか?どのように?見つからなかったので例を挙げてください。SQLiteOpenHelperデータベースをアセットフォルダーからAndroidの/data/ data / MY_PACKAGE /database/フォルダーにコピーするためにオーバーライドすることができました。CursorAdapterカスタムを使用してSQLiteOpenHelperからカーソルを返すカスタムを作成しましたrunQueryOnBackgroundThread。一部の_id列が欠落しているという奇妙なエラーが発生します。テーブルに_id列を追加しました。また、Filterableインターフェイスが何をしているのか、いつデータがフィルタリングされるのかがわかりません。どのメソッド/クラスをオーバーライドする必要がありますか?ありがとう。

4

1 に答える 1

7

できます。

ここからSQLiteOpenHelperが必要です。基本的に、データベースをアセットフォルダから特定のフォルダにコピーする必要があります。次に、カスタム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);
    }

これがカーソルアダプターです。構築時にカーソルに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;
    }

...

}

カスタムコンテンツプロバイダーを作成することもできますが、この場合、オーバーライドする必要があるもう1つの役に立たないクラスになります。

于 2011-03-28T11:31:35.383 に答える