Clicked drop-down item in AutoCompleteTextView does not respond on the first click

拥有回忆 提交于 2019-12-07 16:47:05

问题


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.


回答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 ;)



来源:https://stackoverflow.com/questions/30763879/clicked-drop-down-item-in-autocompletetextview-does-not-respond-on-the-first-cli

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