3

My app implements a HashMap, such that when a key is typed in the AutoCompleteTextView editText, and user clicks a dropdown item, the value gets displayed on a textView.

The only problem is that the clicked drop-down item does not respond on the first click (this only happens once, after the app is launched but works fine subsequently), the user MUST re-type a key and click the item before it displays the value.

Any suggestions as to 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 treat buttons, but not AutoCompleteTextView drop-down items. I've also tried setting focus both in my JAVA and XML but those didn't resolve the issue.

Emzor
  • 1,380
  • 17
  • 28

1 Answers1

1

I didn't understand very well what you are trying to do, but looking in your code, I saw a couple of wrong things in it. Perhaps it is the problem. Moreover I added some lines into your code, so the here is it:

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 do any change in order to realize if the issue was fixed. Then you do whatever you wanna do ;)

Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51
  • I've tried your approach but luck with that. Not sure you understand what I'm trying to achieve. My case is similar to the following links except theirs is button and mine's a dropdown item: http://stackoverflow.com/questions/20755322/button-listener-not-get-registered-on-first-click-in-android http://stackoverflow.com/questions/5919849/android-button-needs-two-click-for-action http://stackoverflow.com/questions/28502091/android-button-only-working-at-second-time-click I've tried these approaches but no luck still. – Emzor Jun 11 '15 at 15:23
  • As I said in my answer, you've made some mistakes into your code, for example, there are two `AutoCompleteTextView ` into your code and you are using just one. Maybe that is the problem. Pay attention at the code I've answered and see I removed one of them and did something more. If it doesn't work, post your entire code so that I will manager to help you. – Lennon Spirlandelli Jun 11 '15 at 18:16