String[] idArray = {"1","2","3","4"};
String[] nameArray = {"Abraham","Abhi","John","Joseph"};
final ArrayAdapter<String> adapterTextView = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,nameArray);
etItemName.setAdapter(adapterTextView);
etItemName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String id = idArray[position];
}
});
这是我的代码。在此,当我输入“Jo”时, John 和 Jospeh 会在AutoCompleteTextView
上填充,当我选择 John 时,我会{ {1}}为 0 。但实际上我需要的是 2 。那么如何才能获得 2 而不是 0 ?
答案 0 :(得分:2)
这不是完美的解决方案,但您可以将其作为最后一个选项:
当您传递所选字符串时,只需循环遍历nameArray即可找到其位置:
int position=-1;
for(int i=0;i<nameArray.length;i++){
if(selectedItem.equalsIgnoreCase(nameArray[i])){
position=i;
}
}
答案 1 :(得分:1)
AutoCompleteTextView t;
t.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int pos = -1;
for(int i = 0; i < nameArray.length; i++){
if(((AutoCompleteTextView)view).getText().toString().equals(nameArray[position]))
pos = position;
break;
}
String id = idArray[pos]; // should be your desired number
}
});