Autocompletetextview with custom adapter and filter

后端 未结 4 1581
梦毁少年i
梦毁少年i 2020-11-27 20:08

I am trying to set a custom ArrayAdapter for my AutoCompleteTextView like this

public class AutoCompleteContactArrayAdapter extend         


        
4条回答
  •  佛祖请我去吃肉
    2020-11-27 20:18

    Okay i think i figure out what Luksprog was saying this code works now they key is this

    mContactList = (ArrayList>) results.values;
    

    in

    @Override
    public int getCount(){
        return mContactList.size();
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.auto_contact_list, parent,
                false);
        TextView nameView = (TextView) rowView.findViewById(R.id.ccontName);
        TextView phoneView = (TextView) rowView.findViewById(R.id.ccontNo);
        TextView typeView = (TextView) rowView.findViewById(R.id.ccontType);
        Map contactMap = mContactList.get(position);
    
        nameView.setText(contactMap.get("name"));
        phoneView.setText(contactMap.get("phone"));
        typeView.setText(contactMap.get("type"));
    
        return rowView;
    }
    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
    
                if (results.count > 0) {
                    mContactList = (ArrayList>) results.values;
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
    
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                ArrayList> result = new ArrayList>();
                HashMap myMap = new HashMap();
                myMap.put("name", "key");
                result.add(myMap);
                HashMap myMap2 = new HashMap();
                myMap2.put("name", "is");
                result.add(myMap2);
                HashMap myMap3 = new HashMap();
                myMap3.put("name", "another");
                result.add(myMap3);
                FilterResults r = new FilterResults();
                r.values = result;
                r.count = result.size();
                return r;
            }
        };
    }
    

提交回复
热议问题