采用Android AutoCompleteTextView与ArrayAdapter<对象和GT;而不是ArrayAdapter<字符串> [英] Using Android AutoCompleteTextView with ArrayAdapter<Objects> instead of ArrayAdapter<Strings>

查看:241
本文介绍了采用Android AutoCompleteTextView与ArrayAdapter<对象和GT;而不是ArrayAdapter<字符串>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Andr​​oid application.I知道如何处理字符串的简单的数组用它来使用AutoCompleteTextView,但我想AutoCompleteTextView使用对象名单进行completion.My code这是以下内容:

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:

活动code

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
}

学生 Object类有关于学生是信息 ID,姓名,地址,商标

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

我知道AutoCompleteTextView需要的String类型的对象的数组执行搜索oper​​ation.In我来说,我想AutoCompleteTextView用我的ArrayList到学生对象字段NAME.I的基础上执行完成不知道我应该怎么指定AutoCompleteTextView使用学生object.Please名外地帮我提供任何链接或一个小例子。

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. 覆盖的toString()学生类,并使其返回名称
    你可以得到与下面code选定的对象:

  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&LT;学生&GT; 类)检查此教程: 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&LT;对象和GT;而不是ArrayAdapter&LT;字符串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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