AutoCompleteTextView的使用

AutoCompleteTextView  autoTextView;

一、简单使用Adapter

String[] gxy_yws = new String[] { "缬沙坦片", "复方利血平片", "复方利血平氨苯蝶啶片", "硝苯地平缓释片", "硝苯地平控释片", "卡托普利片", "尼莫地平片",
            "马来酸依那普利片", "苯磺酸氨氯地平片", "吲达帕胺片", "依那普利片", "心普利片", "心胺利片", "硝苯地平片", "尼群地平片", };
   
// 创建一个ArrayAdapter,封装数组
        ArrayAdapter arr = new ArrayAdapter(getActivity(), android.R.layout.simple_dropdown_item_1line,
                gxy_yws);

        autoTextView.setAdapter(arr);



二、使用自定义的Adapter====Adapter 继承BaseAdapter 并实现Filterable 接口 (HospitalAutoCompleteAdapter mAdapter

1、设置数据 ;

1)autoTextView.setThreshold(1); //设置第一个字符就开始检索


2)mAdapter = new HospitalAutoCompleteAdapter(getActivity());

autoTextView.setAdapter(mAdapter);


3)mAdapter.setDataList(dataBean.getInfor());

mAdapter.notifyDataSetChanged();

4)设置点击事件

注意:获取值的时候的值为  mAdapter.getItem(position)

 autoTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                if (dataBean != null && dataBean.getInfor().size() > 0) {
                    HospitalAndDepartmentBean.InforBean pc = mAdapter.getItem(position);
                    tvPatientInhospital.setText(pc.getOfficeName());
                } else {
                    tvPatientInhospital.setText("");
                }
            }
        });

5)Adapter 

/**
 * 医院adapter
 */
public class HospitalAutoCompleteAdapter extends BaseAdapter implements Filterable {

    private Context m_context;
    private LayoutInflater m_Inflater = null;

    public List datas;
    private ArrayFilter mFilter;
    private ArrayList mUnfilteredData;


    public HospitalAutoCompleteAdapter(Context context) {
        m_context = context;
        m_Inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (datas == null || datas.isEmpty()) {
            return 0;
        }
        return datas.size();
    }

    @Override
    public HospitalAndDepartmentBean.InforBean getItem(int position) {
        if (position < 0 || position >= datas.size()) {
            return null;
        }
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        HospitalAutoCompleteAdapter.ViewHolder holder = null;
        if (convertView == null) {
            holder = new HospitalAutoCompleteAdapter.ViewHolder();
            convertView = m_Inflater.inflate(R.layout.item_simple_param, parent, false);
            holder.tv_public_item_simple = (TextView) convertView.findViewById(R.id.tv_public_item_simple);
            convertView.setTag(holder);
        } else {
            holder = (HospitalAutoCompleteAdapter.ViewHolder) convertView.getTag();
        }

        final HospitalAndDepartmentBean.InforBean dataBean = datas.get(position);
        holder.tv_public_item_simple.setText(dataBean.getOfficeName());

        return convertView;
    }

    /**
     * 设置列表数据
     *
     * @param list
     */
    public void setDataList(List list) {
        datas = new ArrayList();
        datas.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 添加列表数据
     *
     * @param list
     */
    public void addDataList(List list) {
        if (datas == null) {
            setDataList(list);
        } else {
            datas.addAll(list);
            notifyDataSetChanged();
        }
    }

    public List getData() {
        return datas;
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }


    private class ViewHolder {
        TextView tv_public_item_simple;
    }

    private class ArrayFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            if (mUnfilteredData == null) {
                mUnfilteredData = new ArrayList(datas);
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList list = mUnfilteredData;
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList unfilteredValues = mUnfilteredData;
                int count = unfilteredValues.size();

                ArrayList newValues = new ArrayList(count);

                for (int i = 0; i < count; i++) {
                    HospitalAndDepartmentBean.InforBean pc = unfilteredValues.get(i);
                    if (pc != null) {

                        if (pc.getOfficeName() != null && pc.getOfficeName().startsWith(prefixString)) {

                            newValues.add(pc);
                        }
//                        else if(pc.getEmail()!=null && pc.getEmail().startsWith(prefixString)){
//
//                            newValues.add(pc);
//                        }
                    }
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            //noinspection unchecked
            datas = (List) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }
}

 
  

 

你可能感兴趣的:(Android,Android,基本控件)