如果用户没有从我的自动完成文本视图中选择建议,我希望只允许他们单击按钮。
为了实现这一点,我在适配器中实现了一个onKeyListener,并删除了通过适配器设置的标签,如果您按下了建议。然后我检查是否有标签。
但是,onKeyListener似乎无法正确删除标签:
public class StopCursorAdapter extends CursorAdapter{
private Context context;
private LayoutInflater inflater;
private AutoCompleteTextView autoCompleteTextView;
public StopCursorAdapter(final AutoCompleteTextView autoCompleteTextView, Context context, Cursor c){
super(context, c);
this.context = context;
this.autoCompleteTextView = autoCompleteTextView;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.autoCompleteTextView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event){
StopCursorAdapter.this.autoCompleteTextView.setTag(null);
Log.d("cursor", "Removed tag");
Log.d("cursor", String.valueOf(StopCursorAdapter.this.autoCompleteTextView.getTag() == null));
Log.d("cursor", String.valueOf(autoCompleteTextView.getTag() == null));
return false;
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
View v = inflater.inflate(android.R.layout.two_line_list_item, null);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor){
TextView txt1 = (TextView) view.findViewById(android.R.id.text1);
TextView txt2 = (TextView) view.findViewById(android.R.id.text2);
txt1.setTextColor(Color.BLACK);
txt1.setText(cursor.getString(2));
txt2.setText(cursor.getString(3));
}
@Override
public CharSequence convertToString(Cursor cursor){
autoCompleteTextView.setTag(new Stop(cursor.getString(1), cursor.getString(2), cursor
.getString(3)));
return cursor.getString(2);// + ", " + cursor.getString(3);
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint){
Database.getInstance().ensureLoaded(context);
String filter = "";
if(constraint == null){
filter = "";
}else{
filter = constraint.toString();
}
Cursor cursor = Database.getInstance().getStopsCursor(filter);
return cursor;
}
}
有没有其他解决这个问题的方法?