0

我正在过滤一个 Arrayadapter。我使用自定义数组适配器。现在我想过滤,我需要一个自定义过滤器。我遵循了许多示例之一,但我仍然遇到问题。每当我的代码如下所示时,过滤时什么都不会发生。我认为这是因为 getView,然后是最后一行:

textView.setText(original.get(position))

我认为这应该是:

textView.setText(fItems.get(position))

但是当我把它改成这个时,我得到以下错误:

03-14 21:27:19.541: E/AndroidRuntime(25128): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

在这条线上。所以或者它崩溃,或者不起作用。有人知道我在做什么错吗?

这是我的代码:

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

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;

public class SimpleListAdapter extends ArrayAdapter<String>{
    private final Context context;
    private Filter filter;
    private List<String> fItems;
    private final List<String> original;

    public SimpleListAdapter(Context context, String[] values) {
        super(context, R.layout.view_listitem, values);
        this.context = context;

        original = new ArrayList<String>();
        for(int i=0;i<values.length;i++) {
            original.add(values[i]);
        }
        fItems = new ArrayList<String>(original);


    }



    @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.view_listitem, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.tv_listitem);
        textView.setText(original.get(position)); //fItems.get(position?/!)


        return rowView;
    }

    @Override
    public String getItem(int pos) {
        return original.get(pos); //fItems.get(position?/!)
    }




    @Override
    public Filter getFilter()
    {
        if (filter == null)
            filter = new StringNameFilter();

        return filter;
    }

    private class StringNameFilter extends Filter
    {
        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {   
            FilterResults results = new FilterResults();
            String prefix = constraint.toString().toLowerCase();

            if (prefix == null || prefix.length() == 0)
            {
                ArrayList<String> list = new ArrayList<String>(original);
                results.values = list;
                results.count = list.size();
            }
            else
            {
                final ArrayList<String> list = new ArrayList<String>(original);
                final ArrayList<String> nlist = new ArrayList<String>();
                int count = list.size();

                for (int i=0; i<count; i++)
                {
                    final String String = list.get(i);
                    final String value = String.toLowerCase();

                    if (value.startsWith(prefix))
                    {
                        nlist.add(String);
                    }
                }
                results.values = nlist;
                results.count = nlist.size();
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            fItems = (ArrayList<String>)results.values;

            fItems.clear();
            int count = fItems.size();
            for (int i=0; i<count; i++)
            {
                String String = (String)fItems.get(i);
                fItems.add(String);
            }
        }

    }

} 
4

0 回答 0