第一次单击时,AutoCompleteTextView中单击的下拉项不响应

时间:2015-06-10 17:59:16

标签: android autocompletetextview onitemclicklistener onitemclick

我的应用实现了HashMap,以便在key AutoCompleteTextView中输入editText,并且用户点击dropdown项时,该值显示在textView上。

唯一的问题是点击的下拉项目在第一次点击时没有响应(这只发生一次,在应用程序启动后但随后工作正常),用户必须重新键入key并在显示值之前单击该项目。

有关如何解决此问题的任何建议?

Java代码:

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

我见过的大多数相关解决方案都是处理按钮,而不是AutoCompleteTextView下拉项。我也试过在我的JAVA和XML中设置焦点,但那些并没有解决问题。

1 个答案:

答案 0 :(得分:1)

我不太清楚你想要做什么,但是看看你的代码,我看到了一些错误的东西。也许这就是问题所在。此外,我在你的代码中添加了一些代码,所以就是这样:

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

            }

        }

    }

}

在进行任何更改之前对其进行测试,以便了解问题是否已修复。然后你做任何你想做的事情;)