AutoCompleteTextView mActv = (AutoCompleteTextView) findViewbyId(R.id.m_actv);
ArrayAdapter<String> AutoCompleteAdapter = new ArrayAdapter<String>(this,
R.layout.dropdown_text, Names);
mActv.setAdapter(AutoCompleteAdapter);
名称是一个字符串数组。
是否可以获取从下拉列表中选择的文本的索引?
谢谢。
答案 0 :(得分:6)
答案 1 :(得分:6)
只需将OnItemClickListener
(对于点击的项目)或OnItemSelectedListener
(对于使用轨迹球选择的项目,向上/向下键)添加到AutoCompleteTextView
mActv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
String item = arg1.getItemAtPosition(pos);
//your stuff
}
});
答案 2 :(得分:2)
为AutoCompleteTextView实现onItemClickListener,然后在列表中使用indexOf查找所选项目的索引。
actvCity.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int index = cityNames.indexOf(actvCity.getText().toString());
// Do Whatever you want to do ;)
}
});
答案 3 :(得分:0)
Item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//// id contains item if from database
ItemNoSelected = id;
}
});
id来自数据库,我们可以使用它