我想在我的安卓应用程序中使用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需要一个String类型数组对象来执行搜索操作。在我的情况下,我希望AutoCompleteTextView使用我的ArrayList根据Student对象字段NAME执行完成操作。我不知道如何指定AutoCompleteTextView使用Student对象的NAME字段。请帮助我提供任何链接或一个简单的示例。
谢谢