I only wan't the user to be able to click on a button if they have pressed a suggestion from my autocompleteTextview.
To accomplish this, i implemented an onKeyListener im my adapter and removed the tag which was set by the adapter if you pressed a suggestion. Then i checked if there was tag.
But the onKeyListener does not seem to remove the tag properly:
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;
}
}
Is there any other way to solve this issue?
Looking at your code:
setTag(new Stop(...))
is called from the adapter'sconvertToString()
method which will be called by the AutoCompleteTextView when it is building the selection list (for each row in the cursor) and also when it is performing the completion. I don't think this is what you want.setTag(null)
is called from the listener'sOnKey()
method which will be called when the user tap on the keyboard keys. I also don't think this is correct.
I think the correct code should be something similar to this instead:
// set tag to non-null when key is pressed
autoCompleteTextView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
autoCompleteTextView.setTag(new Stop());
return false;
}
});
// set tag to null when an item is tapped
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> p, View v, int pos, long id) {
autoCompleteTextView.setTag(null);
}
});
You can use the setOnClickListner method of the auto complete text view. item selected listener will fire when the list item gains focus & not on click(selection) of the item.
//set on click listener on the auto complete text view
autoField.setOnItemClickListener(autoItemSelectedListner);
and then,
private OnItemClickListener autoItemSelectedListner = new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
//extract selected
selected = adapter.getItem(arg2);
}
};
I hope this helps..
Hmm it don't understand why you are using the tag. Perhaps I am missing a constraint. Otherwise, I think this is what you are going after:
autoCompleteTextView.setOnItemSelectedListener(new OnItemSelectedListener() {
@override
void public onItemSelected(AdapterView<?> parent, View view, int position, long id) {
makeSpecialButtonAvailable();
}
@override
void public onNothingSelected(AdapterView<?> parent) {
//ignore
}
});
来源:https://stackoverflow.com/questions/8135155/autocompletetextview-detect-if-user-choosed-suggestion