How to add a custom adapter to an AutoCompleteTextView

前端 未结 6 992
余生分开走
余生分开走 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:18

    According to the documentation, the inferred type of setAdapter in AutoCompleteTextView is :

     void setAdapter(T adapter)
    

    Your adapter must be a ListAdapter (which BaseAdapter is, so far so good) and a Filterable, which BaseAdapter is not, nor is your Adapter implementation. I would extend an ArrayAdapter, which is Filterable, not to mention is would simplify your implementation (some of your methods duplicate methods of ArrayAdapter for the same result) :

    public class TwoLineDropdownAdapter extends ArrayAdapter {
    
        private LayoutInflater mInflater = null;
        private Activity activity;
    
        public TwoLineDropdownAdapter(Activity a, ArrayList items) {
            super(a, 0, items);
            activity = a;
            mInflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        public static class ViewHolder {
    
            public TextView title;
            public TextView description;
        }
    
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            ViewHolder holder;
    
            if (convertView == null) {
    
                holder = new ViewHolder();
    
                convertView = mInflater.inflate(R.layout.dropdown_text_twoline,
                        parent, false);
                holder.title = (TextView) convertView
                        .findViewById(R.id.text1);
                holder.description = (TextView) convertView
                        .findViewById(R.id.text2);
    
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            return convertView;
        }
    }
    

提交回复
热议问题