在AutoCompleteTextView点击下拉项不会在第一次点击响应会在、AutoCompleteTextView

2023-09-06 14:24:51 作者:相关个性符号推荐>>

我的应用程序实现了一个的HashMap ,例如,当一个中键入 AutoCompleteTextView EDITTEXT ,以及用户点击下拉菜单项,值被上显示的TextView

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:

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

大部分的相关解决方案我见过请客按钮,但不AutoCompleteTextView下拉项目。我也试着将焦点设置无论在我的Java和XML,但这些并没有解决问题。

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.

推荐答案

我不很明白你正在尝试做的,但看着你的code,我看到一对夫妇在错的事情。也许这就是问题所在。而且我加了一些行到code,所以这里是:

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