32

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:

ACTIVITY 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
}

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

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.

Thanks

4

4 回答 4

93

两种方式:

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

     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();
        }
    });
    
  2. 实现自定义适配器(通过扩展BaseAdapter类或ArrayAdapter<Student>类)检查本教程:http ://www.ezzylearning.com/tutorial.aspx?tid=1763429

于 2012-10-25T08:28:37.883 回答
8

您可以使用 anAbstractList来获取String对象列表中每个项目的表示。

private void setupAutoComplete(AutoCompleteTextView view, List<T> objects) {
    List<String> names = new AbstractList<String>() {
        @Override
        public int size() { return objects.size(); }

        @Override
        public String get(int location) {
            return objects.get(location).getName();
        }
    };

    view.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names));
}
于 2016-06-14T22:12:56.947 回答
0

您可以在此适配器中覆盖 getItem,取决于每种类型将其转换为 String

 override fun getItem(position: Int): String? {
        val item = listData[position]
        when (item) {
            is BankItem -> {
                return item.bankName
            }
        ....
 }
于 2020-07-01T04:11:25.417 回答
0

如果您想在 String[] arr=new String[100] 中添加数据;那么它错了。您可以执行与 ArrayList 形式相同的工作,但请记住,您永远不会在此处放置 Getter/Setter 类。只需简单地声明它。请参阅此示例。

在主分区中声明:

 ArrayList<String>arr=new ArrayList<>();

然后以这种方式初始化它:

 for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
                    String imei = jsonObject1.getString("imei");
                    String name = jsonObject1.getString("name");
                    String my_pic = jsonObject1.getString("my_pic");
                    String email = jsonObject1.getString("email");

                    arr.add(name);
                }


                adapter= new ArrayAdapter<>
                        (this, android.R.layout.select_dialog_item, arr);
                autoCompleteText.setThreshold(1);//will start working from first character
                autoCompleteText.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
                autoCompleteText.setTextColor(Color.RED);


            }

我希望这对你有用....

于 2019-09-04T09:09:06.223 回答