1

我在 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);
    }

感谢您的回复,很抱歉发了这么长的帖子。任何提示将不胜感激。

4

1 回答 1

2

您至少没有提到过滤失败的一种情况,因此下面的行更多来自猜测:

我认为您设置了错误的过滤。每次用户输入代码时都不需要设置适配器,更简单的解决方案是在活动类中有一个字段,一个int(或String来自您的代码)将用作查询的一部分。

@Override
public void afterTextChanged(Editable s) {
     mCode = s.toString;
}

接下来,可以像这样改进适配器级别的过滤:

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    if (constraint == null || constraint.toString.equals("")) {
        // this is the contract of this method and should be respected 
        return mDbHelper.getAllCityRecords(); // get all records
    }
    return mDbHelper.getStationCursor(constraint.toString(),
            mCode); // mCode is the field that is updated in the activity class(you should take in consideration that the user could use directly the AutoCompleteTextView, so mCode could be not set at this level)
    return cursor;
}

最后,您可能已经这样做了,但请确保在(直接)查询数据库时得到您期望的结果。由于过滤适用于某些情况,因此可能需要注意。

于 2012-09-13T13:00:05.337 回答