How to get text from AutoCompleteTextView?

前端 未结 9 1712
青春惊慌失措
青春惊慌失措 2020-12-05 04:25

I have an AutoCompleteTextView in my app which works. I have successfully created an onClickItemListener. The question is how to grab the text the

相关标签:
9条回答
  • 2020-12-05 05:06

    There is also a way to get item outside the onItemClick:

    int index = tv.getListSelection();
    if (index != ListView.INVALID_POSITION) {
        Object item = tv.getAdapter().getItem(index);
    }
    

    beware that int getListSelection() may return ListView.INVALID_POSITION if there is no dropdown or if there is no selection.

    0 讨论(0)
  • 2020-12-05 05:17

    Easiest of all

    For Getting text of the selected suggestion in AutoCompleteTextView use this

          autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("========>>", autoCompleteTextView.getText().toString());
            }
        });
    
    0 讨论(0)
  • 2020-12-05 05:17

    To get the text of the displayed item selected by the user

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        String selectedItemText = arg0.getItemAtPosition(arg2);
        Log.i("myTag", "SELECTED TEXT WAS["+selectedItemText+"]);
    }
    

    Following best practices, please use some form of descriptive nomenclature for your variables, your code will make more sense:

    public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int selectedItemIndexIn, long id) 
    
    0 讨论(0)
提交回复
热议问题