Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoCompleteTextView detect if user choosed suggestion

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?

like image 700
Richard Avatar asked Dec 29 '25 15:12

Richard


1 Answers

Looking at your code:

  • setTag(new Stop(...)) is called from the adapter's convertToString() 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's OnKey() 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);
        }
    });
like image 122
Joe Avatar answered Dec 31 '25 06:12

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!