How to get text from AutoCompleteTextView?

那年仲夏 提交于 2019-11-28 05:49:11

arg0 being your AdapterView and arg2 the position.

Have you tried:

arg0.getItemAtPosition(arg2);

Yeah... unfortunately the name of the parameters on the onItemClick method you must implement are not so self-descriptive but here is an example with the names of what they are:

autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
        String selection = (String)parent.getItemAtPosition(position);
        //TODO Do something with the selected text
    }
});
  • parent The AdapterView where the click happened.
  • view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
  • position The position of the view in the adapter
  • id The row id of the item that was clicked.

For more info see: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

Ammar Bukhari

I think what you are looking for is this.

String s = this.mCountry.getEditableText().toString();

Where mCountry is the AutoCompleteTextView.

this.mCountry = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    ArrayAdapter<String> adapterCountry = new ArrayAdapter<String>(this, R.layout.list_item, countries);
    this.mCountry.setAdapter(adapterCountry);

mCountry is the list of countries, and I wanted to save the country selected in SharedPreferences.

Hope this helps.

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

try this:

txtPurpose.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            Purpose selected = (Purpose) arg0.getAdapter().getItem(arg2);
            txtPurpose.setTag(selected);
        }
    });
Jasmine John

One another of getting text of suggestion selected in AutoCompleteTextView is

@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
{
    TextView txtvw=(TextView) view;
    String str=txtvw.getText().toString();
    int index = contactNames.indexOf(str);
} 

Here is the code that will solve the problem.

 private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        int index = (int) view.getTag();
        Object item = parent.getItemAtPosition(index);
        if (item instanceof SearchItemShareConnectionDAO) {
            SearchItemShareConnectionDAO dao = (SearchItemShareConnectionDAO) item;

        }
    }
};

SetTag(Dao.getPosition) in getView() method of adapter.

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) 

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.

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