C
C
cfmm2016-08-13 21:16:30
Android
cfmm, 2016-08-13 21:16:30

Glitch getText() in AutoCompleteTextView?

In the onItemClick event for the AutoCompleteTextView, you need to get the text of the AutoCompleteTextView itself.
It doesn't work for me, AutoCompleteTextView.getText().toString() returns some nonsense:

AutoCompleteTextView mEditText = (AutoCompleteTextView) findViewById(R.id.myEditText);

mEditText.setAdapter(new MyAdapterCompleteItem()); // adapter with CompleteItems

mEditText.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

   CompleteItem myitem = (CompleteItem) adapterView.getItemAtPosition(position);
   String str = mEditText.getText().toString();
    // здесь str == ‘[email protected]'
 }
}
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2016-08-13
@Scotik

I don't quite understand what you need? if you need the text that became in the AutoCompleteTextView after clicking, then isn't it easier to call the getItem method in the adapter?

final AutoCompleteTextView mEditText = (AutoCompleteTextView) findViewById(R.id.my_edit_text);


        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);

        mEditText.setAdapter(adapter); // adapter with CompleteItems

        mEditText.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                String myitem = (String) adapterView.getItemAtPosition(position);
                String str = mEditText.getText().toString();
                String item = adapter.getItem(position);
                Log.d("ITEM = ", myitem);
                Log.d("STR = " , str);
                Log.d("ITEM IS = ", item);
            }
        });

On the emulator, all 3 methods work, what exactly you need to choose yourself, but my adapter is different from yours, maybe you should pay attention to it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question