AutoCompleteTextView detect if user choosed suggestion

若如初见. 提交于 2019-12-04 19:15:10

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

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
  }


});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!