Java 带有自定义适配器的Android AutoCompleteTextView

Java 带有自定义适配器的Android AutoCompleteTextView,java,android,adapter,android-arrayadapter,Java,Android,Adapter,Android Arrayadapter,我陷入了与此非常相似的情况: 经过数小时的阅读,我终于找到了AutoCompleteTextView使用自定义XML布局填充客户列表(我需要的自定义对象/POJO)的方法,但我发现这个错误似乎是由于索引逻辑导致的: java.lang.IndexOutOfBoundsException:索引:2,大小:2 获取(ArrayList.java:437) 在irisdesigns.studio.jewellery.Model.CustomerAutoAdapter.getView(CustomerA

我陷入了与此非常相似的情况:

经过数小时的阅读,我终于找到了AutoCompleteTextView使用自定义XML布局填充客户列表(我需要的自定义对象/POJO)的方法,但我发现这个错误似乎是由于索引逻辑导致的:

java.lang.IndexOutOfBoundsException:索引:2,大小:2 获取(ArrayList.java:437) 在irisdesigns.studio.jewellery.Model.CustomerAutoAdapter.getView(CustomerAutoAdapter.java:76) 在android.widget.AbsListView.obtainView(AbsListView.java:2365) 在android.widget.DropDownListView.obtainView(DropDownListView.java:305)中 在android.widget.ListView.measureHeightOChildren(ListView.java:1408)上 在android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1257)中 在android.widget.ListPopupWindow.show(ListPopupWindow.java:613)上 在android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1217)中 在android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1086)中 位于android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:1068) 在android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)中 位于android.os.Handler.dispatchMessage(Handler.java:106) 位于android.os.Looper.loop(Looper.java:164) 位于android.app.ActivityThread.main(ActivityThread.java:6494) 位于java.lang.reflect.Method.invoke(本机方法) 位于com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

有人能帮忙吗? 适配器代码

public class CustomerAutoAdapter extends ArrayAdapter implements Filterable {

    private ArrayList<CustomerSuggestion> customerSuggestions, filteredList;


    public CustomerAutoAdapter(Context context, ArrayList<CustomerSuggestion> customerSuggestions) {
        super(context, 0, customerSuggestions);
        this.customerSuggestions = customerSuggestions;
        this.filteredList = customerSuggestions;
    }



    @Nullable
    @Override
    public CustomerSuggestion getItem(int position) {
        return customerSuggestions.get(position);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.litem_customer_autocomplete, parent, false);
        }
        CustomerSuggestion customerSuggestion = filteredList.get(position);
        if (customerSuggestion != null) {
            ((TextView) view.findViewById(R.id.tv_cust_auto_name)).setText(customerSuggestion.getCustomer().getName());
            ((TextView) view.findViewById(R.id.tv_cust_auto_ph)).setText(customerSuggestion.getCustomer().getPh1());
            ((TextView) view.findViewById(R.id.tv_cust_auto_email)).setText(customerSuggestion.getCustomer().getEmail());
        }
        return view;
    }


    @Override
    public Filter getFilter() {
        return filter;
    }


    // Custom Filter implementation for custom suggestions we provide.
    private Filter filter = new Filter() {
        @Override
        public CharSequence convertResultToString(Object resultValue) {
            return ((CustomerSuggestion) resultValue).getCustomer().getName();
        }


        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null && constraint.length()>1) {
                ArrayList<CustomerSuggestion> filteredList = new ArrayList<>();
                for (CustomerSuggestion customerSuggestion : customerSuggestions)
                    if (customerSuggestion.getCustomer().getName().toLowerCase().contains(constraint.toString().toLowerCase()))
                        filteredList.add(customerSuggestion);

                FilterResults filterResults = new FilterResults();
                filterResults.values = filteredList;
                filterResults.count = filteredList.size();
                return filterResults;
            } else
                return new FilterResults();
        }


        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count > 0) {
                filteredList = (ArrayList<CustomerSuggestion>) results.values;
                notifyDataSetChanged();
            }
            else
                filteredList = customerSuggestions;
        }
    };
}
AutoCompleteTextView setOnClickListener:

et_customer_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    cust_key = customerSuggestions.get(position).getCust_key();
                    autoFillData(position);
                    userExists = true;
                }
            });
et_customer_name.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共虚线单击(AdapterView AdapterView,视图视图,内部位置,长l){
cust_key=customerSuggestions.get(position.getCust_key();
自动填充数据(位置);
userExists=true;
}
});
在这行中
CustomerSuggestion CustomerSuggestion=filteredList.get(位置)
尝试使用原始列表,而不是过滤列表,如下所示

CustomerSuggestion customerSuggestion = customerSuggestions.get(position);

经过一些搜索后,我相信会发生这种情况,因为这种适配器的主列表是原始列表,而不是经过筛选的列表,所以会调用get view方法来引用原始列表

我想它会起作用,因为我已经面对了这个问题谢谢@Giulio Pettenuzzo,这似乎已经解决了它。不过,在确认之前,我会再彻底检查一遍。但是我仍然不明白,为什么使用/显示原始列表(customerSuggestions)而不是filteredList?适配器将如何显示过滤结果?@viditkothari我编辑答案给你一个解释,记住向上投票并接受答案,这样你将获得一些分数tooHi@GiulioPettenuzzo感谢更新。正如我所说的,这部分代码现在是无错误的,但它不会过滤结果。每次我开始输入时,都会弹出完整的列表,但我看不到筛选结果?我想我需要了解getfilter和其他可过滤的实现是如何工作的&什么时候工作的,因为有了这种了解,我就能够修复我的代码了。您是否有类似于流程图或适配器生命周期的资源/文章?
CustomerSuggestion customerSuggestion = customerSuggestions.get(position);