如果用户从我的 autocompleteTextview 中按下了建议,我只希望用户无法单击按钮。
为了实现这一点,我在我的适配器中实现了一个 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;
}
}
有没有其他方法可以解决这个问题?