gpt4 book ai didi

java - 使用 SQLiteDatabase 为 AutoCompleteTextView 迁移到 CursorLoader 和 LoaderManager

转载 作者:行者123 更新时间:2023-11-30 00:04:27 24 4
gpt4 key购买 nike

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

尽管如此,我还是被建议往CursorLoader的方向看。和 LoaderManager ,并且我已经尝试过,但无法正常工作。如果有人指导/推荐/展示将我的代码迁移到 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

最佳答案

不幸的是,在 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");
}
}

关于java - 使用 SQLiteDatabase 为 AutoCompleteTextView 迁移到 CursorLoader 和 LoaderManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49254716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com