Autocompletetextview与自定义适配器和过滤器 [英] Autocompletetextview with custom adapter and filter

查看:165
本文介绍了Autocompletetextview与自定义适配器和过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置自定义arrayadapter我autocompletetextview像这样

I am trying to set a custom arrayadapter for my autocompletetextview like this

public class AutoCompleteContactArrayAdapter extends
    ArrayAdapter<Map<String, String>> implements Filterable {
private Context mContext;
private List<Map<String, String>> mContactList;

public AutoCompleteContactArrayAdapter(Context context,
        List<Map<String, String>> objects) {
    super(context, R.layout.auto_contact_list, objects);
    mContext = context;
    mContactList = objects;
    // TODO Auto-generated constructor stub
}

@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<String, String> 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) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            ArrayList<String> result = new ArrayList<String>();
            result.add("test");
            result.add("another");
            result.add("last");
            FilterResults r = new FilterResults();
            r.values = result;
            r.count = result.size();
            return r;
        }
    };
}
}

在调试应用程序正进入过滤器的方法既publishResults和performFiltering但结果集正在显示的不是我的测试数组[测试,另外,最后]相反,它只是显示所有的结果无视我的过滤器

In debug the application is entering the filter methods both publishResults and performFiltering but the result set that is being displayed is not my test array [test,another,last] instead it just shows all the results ignoring my filter

推荐答案

好吧,我想我找出Luksprog在说这个code ++工程,现在他们关键的是这个

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

mContactList = (ArrayList<Map<String, String>>) results.values;

@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<String, String> 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<Map<String, String>>) results.values;
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
            HashMap<String,String> myMap = new HashMap<String,String>();
            myMap.put("name", "key");
            result.add(myMap);
            HashMap<String,String> myMap2 = new HashMap<String,String>();
            myMap2.put("name", "is");
            result.add(myMap2);
            HashMap<String,String> myMap3 = new HashMap<String,String>();
            myMap3.put("name", "another");
            result.add(myMap3);
            FilterResults r = new FilterResults();
            r.values = result;
            r.count = result.size();
            return r;
        }
    };
}

这篇关于Autocompletetextview与自定义适配器和过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆