我正在使用自动完成 TextView ,它显示数据库中的一些名称。我想在我从自动完成 TextView 中选择的 TextView 中显示一个名称。这是我的代码:
ArrayList<String> s1 = new ArrayList<String>();
for (StudentInfo cn : studentInfo) {
s1.add(cn.getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,s1);
a1.setThreshold(1);
a1.setAdapter(adapter);
a1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
最佳答案
这样试试:
AutoCompleteTextView a1 = (AutoCompleteTextView) findViewById(...);
StudentInfo[] s1 = studentInfo.toArray(new StudentInfo[studentInfo.size()]);
ArrayAdapter<StudentInfo> adapter = new ArrayAdapter<StudentInfo>(this, android.R.layout.simple_dropdown_item_1line, s1);
a1.setAdapter(adapter);
a1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
Object item = parent.getItemAtPosition(position);
if (item instanceof StudentInfo){
StudentInfo student=(StudentInfo) item;
doSomethingWith(student);
}
}
});
ArrayAdapter 使用 StudentInfo 的 toString() 方法来生成显示的文本,因此您需要实现一个很好的 toString 方法。
这样,这种实现可以适应任何对象类型。
顺便说一句:我更喜欢 android.R.layout.simple_spinner_dropdown_item 而不是 android.R.layout.simple_dropdown_item_1line
关于java - 自动完成 TextView 的选定项目显示为简单的 TextView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15833327/