How to add a custom adapter to an AutoCompleteTextView

前端 未结 6 1138
感情败类
感情败类 2021-02-14 08:35

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:26

    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.

提交回复
热议问题