Clicking on dropdown item in AutoCompleteTextView does not respond on first click

My application implements HashMap

so that when a key

is entered into AutoCompleteTextView

editText

and the user clicks on the element dropdown

, the value is mapped to textView

.

The only problem is that the clicked dropdown does not respond to the first click (this only happens once, after the application is started, but works well afterwards), the user MUST re-enter a key

and click the item before it displays the value.

Any suggestions on how to resolve this?

Java code:

public class MainActivity extends ActionBarActivity 
{   

Map<String, String> map = new HashMap<String, String>();

private EditText autocomplete_searchField;
private TextView displayField;
String note;

@Override
protected  void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    autocomplete_searchField = (AutoCompleteTextView) findViewById(R.id.autocomplete_searchField);

    // Gets a reference to the AutoCompleteTextView in the layout
    final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_searchField);

    // Gets the string array
    String[] musicNote = getResources().getStringArray(R.array.music_notes_array);

    // Creates the adapter and set it to the AutoCompleteTextView 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, musicNote);
    textView.setAdapter(adapter);


    displayField = (TextView) findViewById(R.id.displayField);  
    textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
            displayField.setText(map.get(adapterView.getItemAtPosition(position).toString()));

        String note = autocomplete_searchField.getText().toString();
            displayField.setText(map.get(note));


        map.put("Doe", "a deer, a female deer");
        map.put("Ray", "a drop of golden sun");
}

      

Most of the related solutions I've seen are for buttons, but not for AutoCompleteTextView dropdowns. I also tried setting focus in both JAVA and XML, but that didn't fix the problem.

+2


source to share


1 answer


I didn't understand very well what you are trying to do, but looking at your code I saw a couple of wrong things. This may be the problem. Also, I added some lines to your code, so here it is:

public class MainActivity extends ActionBarActivity {  

    private Map<String, String> map = new HashMap<String, String>();
    private AutoCompleteTextView autocomplete_searchField;
    private TextView displayField;

    @Override
    protected  void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        autocomplete_searchField = (AutoCompleteTextView) findViewById(R.id.autocomplete_searchField);
        displayField = (TextView) findViewById(R.id.displayField);

        // Gets the string array
        String[] musicNote = getResources().getStringArray(R.array.music_notes_array);

        // Creates the adapter and set it to the AutoCompleteTextView 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, musicNote);
        autocomplete_searchField.setAdapter(adapter);

        autocomplete_searchField.setThreshold(1);

        autocomplete_searchField.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                autocomplete_searchField.showDropDown();

            }

        });

        autocomplete_searchField.setOnItemClickListener(new AdapterView.OnItemClickListener() {


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

                displayField.setText(map.get(adapterView.getItemAtPosition(position).toString()));

            }

        }

    }

}

      



Test it before making any changes to see if the problem is fixed. Then you do whatever you want;)

+1


source







All Articles