适配器筛选期间的java.lang.IndexOutOfBoundsException

时间:2014-06-29 21:43:48

标签: java android filtering android-arrayadapter

这部分代码应该过滤适配器中的对象,并根据用户输入删除或添加listview中的对象。但是,当我尝试输入任何字母时,我收到一条错误消息:java.lang.IndexOutOfBoundsException:索引5无效,大小为5或类似,具体取决于EditText中的输入。我不明白是什么原因造成的。

以下是代码:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import nick.miros.BudgetControl.budgetcontrol.app.Currency;
import nick.miros.BudgetControl.budgetcontrol.app.R;

public class CurrencyAdapter extends ArrayAdapter<Currency> {
    private final Context context;
    private List<Currency> currencies;
    private CurrencyFilter currencyFilter;

    public CurrencyAdapter(Context context, List<Currency> currencies) {
        super(context, R.layout.list_currency, currencies);
        this.context = context;
        this.currencies = currencies;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_currency, parent, false);

        TextView currencyName = (TextView) rowView.findViewById(R.id.currencyName);
        TextView currencySymbol = (TextView) rowView.findViewById(R.id.currencySymbol);

        currencyName.setText(currencies.get(position).getCurrencyName());
        currencySymbol.setText(currencies.get(position).getSymbol());

        return rowView;
    }

    @Override
    public Filter getFilter() {
        if (currencyFilter == null)
            currencyFilter = new CurrencyFilter();

        return currencyFilter;
    }


    private class CurrencyFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            // We implement here the filter logic
            if (constraint == null || constraint.length() == 0) {
                // No filter implemented we return all the list
                results.values = currencies;
                results.count = currencies.size();
            }
            else {
                // We perform filtering operation
                List<Currency> nCurrencyList = new ArrayList<Currency>();

                for (Currency c : currencies) {
                    if (c.getCurrencyName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                        nCurrencyList.add(c);
                }

                results.values = nCurrencyList;
                results.count = nCurrencyList.size();

            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {

            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0)
                notifyDataSetInvalidated();
            else {
                currencies = (List<Currency>) results.values;
                notifyDataSetChanged();
            }
        }
    }
}

以下是我得到的输出:

06-29 23:08:05.369  15570-15570/nick.miros.BudgetControl.budgetcontrol.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
            at java.util.ArrayList.get(ArrayList.java:304)
            at nick.miros.BudgetControl.budgetcontrol.helper.CurrencyAdapter.getView(CurrencyAdapter.java:41)
            at android.widget.AbsListView.obtainView(AbsListView.java:2337)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1289)
            at android.widget.ListView.onMeasure(ListView.java:1201)
            at android.view.View.measure(View.java:15669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4946)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1433)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:720)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:593)
            at android.view.View.measure(View.java:15669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4946)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:326)
            at android.view.View.measure(View.java:15669)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:872)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:593)
            at android.view.View.measure(View.java:15669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4946)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:326)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2214)
            at android.view.View.measure(View.java:15669)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2232)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1316)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1510)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1206)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5004)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
            at android.view.Choreographer.doCallbacks(Choreographer.java:579)
            at android.view.Choreographer.doFrame(Choreographer.java:548)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)

指向这一行:

currencyName.setText(currencies.get(position).getCurrencyName());

0 个答案:

没有答案