【发布时间】:2012-10-15 08:33:58
【问题描述】:
我想在我的 android 应用程序中使用 AutoCompleteTextView。我知道如何将它与简单的字符串数组一起使用,但我希望 AutoCompleteTextView 使用对象列表来执行完成。我的代码如下:
活动代码
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
initialize();
ArrayAdapter<Student> adapter = new ArrayAdapter<Student>(this,
R.layout.dropdown_list_item, GetAllStudentsList());
searchBox.setAdapter(adapter);
searchBox.setThreshold(THRESHOLD_VALUE);
searchBox.setTextColor(Color.BLACK);
}
searchBox.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long arg3) {
//Here i will grab the Student object that user selected from drop-down
}
});
public ArrayList<Movies> GetAllStudentsList() {
//This method returns a ArrayList of <Student> type objects
}
Student 类对象包含有关ID,NAME,ADDRESS,MARKS 的学生的信息。
我知道 AutoCompleteTextView 需要一个字符串类型对象的数组来执行搜索操作。在我的情况下,我希望 AutoCompleteTextView 使用我的 ArrayList 来根据学生对象字段 NAME 执行完成。我不知道应该如何指定 AutoCompleteTextView 使用Student 对象的 NAME 字段。请帮助我提供任何链接或小示例。
谢谢
【问题讨论】:
-
我想我需要自定义 AutoCompleteTextView 并覆盖一些方法..
标签: android autocomplete android-view android-adapter