过滤适配器后的IndexOutOfBoundsException

问题描述 投票:0回答:1

我有一个自动完成控件,它将从api中绘制以构建自动完成下拉列表。我已将所有内容都连线,但输入时出现此错误:

03-26 13:41:40.546 E / AndroidRuntime(31931):java.lang.IndexOutOfBoundsException:Index:0,Size: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):at 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):at 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(Native Method) )> 03-26 13:41:40.546 E / AndroidRuntime(31931):at com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)03-26 13:41:40.546 E / AndroidRuntime( 31931):at 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控件。

xamarin.android autocompletetextview
1个回答
0
投票

我终于搞定了。关键问题是我的适配器使用的是ArrayAdapter而不是BaseAdapter

一旦我切换出来,它按预期工作,不知道为什么我不能使用ArrayAdapter但我很高兴我的自动完成控制终于工作。

© www.soinside.com 2019 - 2024. All rights reserved.