How to add a custom adapter to an AutoCompleteTextView

前端 未结 6 971
余生分开走
余生分开走 2021-02-14 08:37

Is there any simple way to set a 2 TextView dropdown to an AutoCompleteTextView.

There is android.R.layout.two_line_list_item Which I couldn\'t find any exa

6条回答
  •  别跟我提以往
    2021-02-14 09:29

    I believe that the easiest approach is to extend SimpleAdapter.

    public class MyAdapter extends android.widget.SimpleAdapter {
    
        static ArrayList> toMapList(Collection objectsCollection) {
            ArrayList> objectsList = new ArrayList>(objectsCollection.size());
            for (MyObject obj : objectsCollection) {
                Map map = new HashMap();
                map.put("name", obj.getName());
                map.put("details", obj.getDetails());
                objectsList.add(map);
            };
            return objectsList;
        }
    
        public MyAdapter(Context context, Collection objects) {
    
            super(context, toMapList(objects),
                  R.layout.auto_complete_layout, new String[] {"name", "description"}, new int[] {R.id.name, R.id.description});
        }
    }
    

    The major drawback is that this will bring candidates based on any space-delimited word in either name or description. If you add another field to your auto_complete_layout, it will be involved in matching, too.

    Therefore, I finished with rewriting the SimpleAdapter to better fit my needs, removing a significant amount of the base class overhead which was not relevant for my use case. But the few lines above give you a good start, and provide a solid reference to start customization from.

提交回复
热议问题