我的应用程序实现了一个HashMap
,这样当在AutoCompleteTextView
editText
中输入key
时,用户单击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中设置焦点,但这些都没有解决问题。
我不太明白你想做什么,但是在你的代码中,我看到了一些错误的东西。也许这就是问题所在。此外,我在你的代码中添加了一些行,所以这里是:
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()));
}
}
}
}
在做任何更改之前测试它,以了解问题是否已修复。然后你想怎么做就怎么做;)