我在 AutoCompleteTextView 上实现 CursorAdapter 时遇到问题。
ZipCode : _ (<--EditText)
City : _ ___ (<-- AutoCompleteTextView)
基本上,我想帮助用户为输入的邮政编码推荐可用的城市。我的问题是没有显示建议(我猜光标不会启动查询)。我不明白为什么它在某些情况下有效,而在其他情况下无效。我在下面附上故障案例。
我的光标适配器:
public class SearchCursorAdapter extends CursorAdapter {
private DataBaseHelper mDbHelper;
private String codePostal;
public SearchCursorAdapter(DataBaseHelper dbHelper, Context context,
String codePostal) {
// Call the CursorAdapter constructor with a null Cursor.
super(context, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
mDbHelper = dbHelper;
this.codePostal = codePostal;
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
}
Cursor cursor = mDbHelper.getStationCursor(constraint.toString(),
codePostal);
return cursor;
}
@Override
public String convertToString(Cursor cursor) {
return cursor.getString(1); //second column in select
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view).setText(cursor.getString(1)); //second column in select
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.spinner_layout, null);
return view;
}
}
来自 db 适配器的 select 方法:
public Cursor getStationCursor(String args, String arg2) {
StringBuffer sqlQuery = new StringBuffer("");
Cursor result = null;
sqlQuery.append(" SELECT min(_id) as _id, ");
sqlQuery.append(CITIES.CITY);
sqlQuery.append(" FROM ");
sqlQuery.append(CITIES.TABLE_NAME);
sqlQuery.append(" WHERE ");
sqlQuery.append(CITIES.CITY);
sqlQuery.append(" LIKE '");
sqlQuery.append(args);
sqlQuery.append("%' ");
sqlQuery.append("AND ");
sqlQuery.append(CITIES.CODE_POSTAL);
sqlQuery.append(" LIKE '");
sqlQuery.append(arg2);
sqlQuery.append("%' ");
sqlQuery.append(" GROUP BY ");
sqlQuery.append(CITIES.CITY);
sqlQuery.append(" ORDER BY ");
sqlQuery.append(CITIES.CITY);
sqlQuery.append(" LIMIT 10 ");
if (myDataBase != null) {
result = myDataBase.rawQuery(sqlQuery.toString(), null);
}
if (result != null) {
result.moveToFirst();
}
return result;
}
我活动中的代码:
EditText etCodPost;
AutoCompleteTextView acCity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout....);
etCodPost = (EditText) ...;
acCom = (AutoCompleteTextView) ...;
setComAdapter(activity);
etCodPost.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
setComAdapter(activity);
}
});
}
private void setComAdapter(Activity activity) {
SearchCursorAdapter adapt = new SearchCursorAdapter(myDbHelper, activity,
etCodPost.getText().toString());
acCity.setAdapter(adapt);
acCity.setThreshold(3);
}
感谢您的回复,很抱歉发了这么长的帖子。任何提示将不胜感激。