过滤适配器后出现IndexOutOfBoundsException

时间:2019-03-26 18:10:38

标签: xamarin.android autocompletetextview

我有一个自动完成控件,该控件将从api绘制以构建自动完成下拉列表。我已经把所有东西都整理好了,但是在输入时出现了这个错误:

  

03-26 13:41:40.546 E / AndroidRuntime(31931):java.lang.IndexOutOfBoundsException:索引:0,大小:0   03-26 13:41:40.546 E / AndroidRuntime(31931):at java.util.ArrayList.get(ArrayList.java:437)   03-26 13:41:40.546 E / AndroidRuntime(31931):位于android.widget.ArrayAdapter.getItem(ArrayAdapter.java:385)   03-26 13:41:40.546 E / AndroidRuntime(31931):位于android.widget.AutoCompleteTextView.buildImeCompletions(AutoCompleteTextView.java:1243)   03-26 13:41:40.546 E / AndroidRuntime(31931):位于android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1203)   03-26 13:41:40.546 E / AndroidRuntime(31931):位于android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1086)   03-26 13:41:40.546 E / AndroidRuntime(31931):位于android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:1068)   03-26 13:41:40.546 E / AndroidRuntime(31931):位于android.widget.Filter $ ResultsHandler.handleMessage(Filter.java:285)   03-26 13:41:40.546 E / AndroidRuntime(31931):位于android.os.Handler.dispatchMessage(Handler.java:106)   03-26 13:41:40.546 E / AndroidRuntime(31931):位于android.os.Looper.loop(Looper.java:164)   03-26 13:41:40.546 E / AndroidRuntime(31931):位于android.app.ActivityThread.main(ActivityThread.java:6494)   03-26 13:41:40.546 E / AndroidRuntime(31931):at java.lang.reflect.Method.invoke(本机方法)>   03-26 13:41:40.546 E / AndroidRuntime(31931):在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)   03-26 13:41:40.546 E / AndroidRuntime(31931):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

我的适配器类

public class UserFilterAdapter : ArrayAdapter<UserAutoComplete>, IFilterable
{
    private readonly Context _context;
    private readonly int _textViewResourceId;
    private readonly Filter _filter;
    private List<UserAutoComplete> _userAutoComplete;

    public override int Count => _userAutoComplete?.Count ?? 0;

    public override Filter Filter => _filter;

    public DirectoryOrgUnitFilterAdapter(List<UserAutoComplete> userAutoComplete, Context context, int textViewResourceId) : base(context, textViewResourceId)
    {
        _context = context;
        _filter = new UserAutoCompleteFilter(this);
        _textViewResourceId = textViewResourceId;
        _userAutoComplete = userAutoComplete;
    }

    public void RefreshAdapter(List<UserAutoComplete> userAutoComplete)
    {
        _userAutoComplete = userAutoComplete;

        NotifyDataSetChanged();
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            var inflater = (LayoutInflater) _context.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(_textViewResourceId, parent, false);
        }

        var itemName = convertView.FindViewById<TextView>(Resource.Id.user_autocomplete_item_name);
        var itemType = convertView.FindViewById<TextView>(Resource.Id.user_autocomplete_item_type);
        var item = GetItem(position);

        itemName.Text = item.UserName;
        itemType.Text = item.Email;

        return convertView;
    }
}

我的过滤器类

public class UserAutoCompleteFilter : Filter
{
    private readonly UserFilterAdapter _adapter;

    public UserAutoCompleteFilter(UserFilterAdapter adapter)
    {
        _adapter = adapter;
    }

    protected override FilterResults PerformFiltering(ICharSequence constraint)
    {
        var results = new FilterResults();

        if (string.IsNullOrEmpty(constraint?.ToString()))
        {
            return results;
        }

        var searchFor = constraint.ToString();

        if (searchFor.Length < 3)
        {
            return results;
        }

        var matches = ApiService.GetUsers(searchFor).ConfigureAwait(false).GetAwaiter().GetResult();

        results.Values = FromArray(matches.Select(r => r.ToJavaObject()).ToArray());
        results.Count = matches.ToList().Count;

        constraint.Dispose();

        return results;
    }

    protected override void PublishResults(ICharSequence constraint, FilterResults results)
    {
        if (results?.Values == null)
        {
            return;
        }

        using (var values = results.Values)
        {
            _adapter.RefreshAdapter(values.ToArray<Object>().Select(a => a.ToNetObject<UserAutoComplete>()).ToList());
        }

        constraint.Dispose();
        results.Dispose();
    }
}

我通过逻辑进行了调试,并且过滤器被命中并返回了数据,所以我迷失了丢失数据的地方并抛出了异常。

我已经按照this教程进行过滤,以使其能够在C#中工作,然后遵循了this教程,将其应用于自动完成的textview控件。

1 个答案:

答案 0 :(得分:0)

我终于开始工作了。关键问题是我的适配器正在使用ArrayAdapter而不是BaseAdapter

一旦我将其关闭,它就会按预期工作,不确定为什么我不能使用ArrayAdapter,但我为我的自动完成控件终于可以正常工作而感到非常兴奋。