0

我的应用程序中有一个这样的向量:

  Vector<Firm> firmVector= new Vector<Firm>();

如您所见,这是我自己的类中的对象向量Firm

所以对于我的问题,是否可以在AutoComplete这个“向量”中添加一个?

像这个,来自developer.android.com

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autocomplete_country);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);
  textView.setAdapter(adapter);
}
4

1 回答 1

1

getFilter()是的,您可以通过实现 ArrayAdapter 来做到这一点,但您需要实现AutoCompleteTextView. 与此类似的东西可能会起作用:

    private class FirmAdapter extends ArrayAdapter<Firm> {
private Filter filter;
        @Override
        public Filter getFilter() 
        {
            if(filter == null){
                filter = new Filter(){
                    int lastlen = 0;
                    boolean initialized = false;

                    @SuppressWarnings("unchecked")
                    @Override
                    protected void publishResults(CharSequence constraint, FilterResults results)
                    {
                        filteredItems = (List<Firm>)results.values;
                        ArrayAdapter.this.notifyDataSetChanged();
                    }

                    @Override
                    protected FilterResults performFiltering(CharSequence constraint)
                    {
                        FilterResults results = new FilterResults();
                        if(constraint == null || constraint.length() == 0){
                            results.values = null;
                        }else{
                            String val;
                            Firm obj;
                            constraint = constraint.toString().toUpperCase();
                            int newlen = constraint.length();
                            LinkedList<Firm> filteredResults = new LinkedList<Firm>();
                            if(newlen < lastlen){
                                int i = 0, size = ArrayAdapter.super.getCount();
                                for(i=0;i<size;i++){
                                    obj = ArrayAdapter.super.getItem(i);
                                    val = obj.toString(); // CUSTOMIZE THIS
                                    if(val.contains(constraint)) filteredResults.add(obj);
                                }
                            }else{
                                int i = 0, size = getCount();
                                for(i=0;i<size;i++){
                                    obj = getItem(i);
                                    val = obj.toString(); // CUSTOMIZE THIS
                                    if(val.contains(constraint)) filteredResults.add(obj);
                                }
                            }
                            lastlen = newlen;
                            results.values = filteredResults;
                        }
                        return results;
                    }
                };
            }
            return filter;
        }
    }
}

我从我的一个项目中删除了这个,所以它需要一些测试和清理才能开始工作(在我的情况下,我在一组JSONObject实例上有一个过滤器,你有一个Firm)但是尝试类似的东西。带有的注释CUSTOMIZE THIS是您希望根据输入到AutoCompleteTextView.

于 2012-05-01T15:04:35.743 回答