迁移到CursorLoader&使用SQLiteDatabase

时间:2018-03-13 11:12:31

标签: java android sqlite cursor android-cursorloader

我有AutoCompleteTextView,其中显示了从SQLiteDatabase查询中获取的建议的下拉列表。目前它使用SimpleCursorAdapter,但是它有几个问题(我在这里有一个关于这个问题的单独问题:SimpleCursorAdapter issue - "java.lang.IllegalStateException: trying to requery an already closed cursor")。

然而,我被建议朝着CursorLoaderLoaderManager的方向前进,而我已经尝试过了,但却无法使其发挥作用。如果有人指导/推荐/展示将我的代码迁移到CursorLoader / LoaderManager概念的正确方法,我将不胜感激。任何形式的帮助非常感谢!

mAdapter = new SimpleCursorAdapter(this,
                R.layout.dropdown_text,
                null,
                new String[]{CITY_COUNTRY_NAME},
                new int[]{R.id.text}, 0);
        mAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);
                cityCountryName = cursor.getString(cursor.getColumnIndexOrThrow(CITY_COUNTRY_NAME));
                mAutoCompleteTextView.setText(cityCountryName);
                JSONWeatherTask task = new JSONWeatherTask();
                task.execute(new String[]{cityCountryName});
            }
        });
        mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence sequence) {
                String constraint = sequence.toString();
                String queryString = "SELECT " + ID + ", " + CITY_ID + ", " + CITY_COUNTRY_NAME + " FROM " + TABLE_1;
                constraint = constraint.trim() + "%";
                queryString += " WHERE " + CITY_COUNTRY_NAME + " LIKE ?";
                String params[] = {constraint};
                try {
                    Cursor cursor = database.rawQuery(queryString, params);
                    if (cursor != null) {
                        startManagingCursor(cursor);
                        cursor.moveToFirst();
                        return cursor;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
        mAutoCompleteTextView.setAdapter(mAdapter);

以下是参考的整个项目(如果您愿意,可以在那里提出拉取请求):Open Weather App

1 个答案:

答案 0 :(得分:1)

不幸的是,在SO上没有人提出解决方案,但是一位同事(他没有SO帐户)提出了一个带有迁移修复程序的拉取请求。它工作得很好,我决定在这里发布正确的答案。

如果您对这些改进在实际代码中的内容感兴趣,欢迎您访问GitHub上的原始开源项目页面:Open Weather App

这是新适配器:

mAdapter = new SimpleCursorAdapter(this,
                R.layout.dropdown_text,
                null,
                new String[]{CITY_COUNTRY_NAME},
                new int[]{R.id.text},0);
        mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
            @Override
            public Cursor runQuery(CharSequence constraint) {
                if (constraint != null) {
                    if (constraint.length() >= 3 && !TextUtils.isEmpty(constraint)) {
                        Bundle bundle = new Bundle();
                        String query = charArrayUpperCaser(constraint);
                        bundle.putString(CITY_ARGS, query);
                        getLoaderManager().restartLoader(0, bundle, MainActivity.this).forceLoad();
                    }
                }
                return null;
            }
        });

这是onCreateLoader()回调:

@Override
public android.content.Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String s = args.getString(CITY_ARGS);
    WeatherCursorLoader loader = null;
    if (s != null && !TextUtils.isEmpty(s)) {
        loader = new WeatherCursorLoader(this, database, s);
    }
    return loader;
}

自定义CursorLoader本身:

private static class WeatherCursorLoader extends CursorLoader {

    private SQLiteDatabase mSQLiteDatabase;
    private String mQuery;

    WeatherCursorLoader(Context context, SQLiteDatabase cDatabase, String s) {
        super(context);
        mSQLiteDatabase = cDatabase;
        mQuery = s + "%";
        Log.d(TAG, "WeatherCursorLoader: " + mQuery);
    }


    @Override
    public Cursor loadInBackground() {
        return mSQLiteDatabase.query(TABLE_1, mProjection,
                CITY_COUNTRY_NAME + " like ?", new String[] {mQuery},
                null, null, null, "50");
    }
} 
相关问题