将 Android AutoCompleteTextView 与 ArrayAdapter<Objects> 结合使用而不是 ArrayAdapter<Strings> [英] Using Android AutoCompleteTextView with ArrayAdapter&lt;Objects&gt; instead of ArrayAdapter&lt;Strings&gt;

查看:33
本文介绍了将 Android AutoCompleteTextView 与 ArrayAdapter<Objects> 结合使用而不是 ArrayAdapter<Strings>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 android 应用程序中使用 AutoCompleteTextView.我知道如何将它与简单的字符串数组一起使用,但我希望 AutoCompleteTextView 使用对象列表来执行完成.我的代码如下:

I wanted to use AutoCompleteTextView in my android application.I know how to use it with simple array of Strings, but I wanted AutoCompleteTextView to use list of Objects to perform completion.My code for this is following:

活动代码

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 的学生的信息.

Student class Object has information regarding a student which is ID,NAME,ADDRESS,MARKS.

我知道 AutoCompleteTextView 需要一个 String 类型对象的数组来执行搜索操作.在我的情况下,我希望 AutoCompleteTextView 使用我的 ArrayList 根据 Student 对象字段 NAME 执行完成.我不知道我应该如何指定 AutoCompleteTextView 使用Student 对象的 NAME 字段.请帮我提供任何链接或一个小例子.

I know AutoCompleteTextView needs an array of String type object to perform search operation.In my case I want AutoCompleteTextView to use my ArrayList to perform completion on the basis of Student object field NAME.I dont know how should I specify AutoCompleteTextView to use NAME field of Student object.Please help me providing any Link or a small example.

谢谢

推荐答案

两种方式:

  1. 覆盖Student 类中的toString() 并使其返回name.您可以使用以下代码获取选择的对象:

  1. Override toString() in Student class and make it return name. You can get the object that was selected with the following code:

 public static class Student {

    private String name;

        public Student(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }

    }

AutoCompleteTextView tv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
final ArrayList<Student> list = new ArrayList<MainActivity.Student>();
list.add(new Student("Viru"));
list.add(new Student("Gauti"));
ArrayAdapter<Student> adapter = new ArrayAdapter<MainActivity.Student>(
        this, android.R.layout.simple_dropdown_item_1line, list);
tv.setAdapter(adapter);

tv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        Student selected = (Student) arg0.getAdapter().getItem(arg2);
        Toast.makeText(MainActivity.this,
                "Clicked " + arg2 + " name: " + selected.name,
                Toast.LENGTH_SHORT).show();
    }
});

  • 实现自定义适配器(通过扩展BaseAdapter 类或ArrayAdapter 类)查看本教程:http://www.ezzylearning.com/tutorial.aspx?tid=1763429

  • Implement a custom adapter (by extending BaseAdapter class or ArrayAdapter<Student> class) Check this tutorial : http://www.ezzylearning.com/tutorial.aspx?tid=1763429

    这篇关于将 Android AutoCompleteTextView 与 ArrayAdapter<Objects> 结合使用而不是 ArrayAdapter<Strings>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆