0

ListView でカスタム アダプターのフィルター処理に問題がある

public class PeopleAdapter extends ArrayAdapter<TUser> implements Filterable {

int resource;
String response;
Context context;

private Filter mFilter;

protected List<TUser> mActivitiesList;

//Initialize adapter
public PeopleAdapter(Context context, int resource, List<TUser> items) {
    super(context, resource, items);
    mActivitiesList = items;
    this.resource=resource;


}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LinearLayout PeopleList;
    //Get the current alert object
    TUser user = getItem(position);

    //Inflate the view
    if(convertView==null)
    {
        PeopleList = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi;
        vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resource, PeopleList, true);
    }
    else
    {
        PeopleList = (LinearLayout) convertView;
    }
    //Get the text boxes from the listitem.xml file
    TextView ContactName =(TextView)PeopleList.findViewById(R.id.txtNameText);
    TextView ContactNick =(TextView)PeopleList.findViewById(R.id.txtNickText);

    //Assign the appropriate data from our alert object above
    ContactName.setText(user.FName + " " + user.Name);
    ContactNick.setText(user.Nick);

    return PeopleList;
}





@Override
public Filter getFilter() {
    if (mFilter == null)
        mFilter = new ArrayFilter();
    return mFilter;     
}

/**
 * An array filters constrains the content of the array adapter with a prefix. Each
 * item that does not start with the supplied prefix is removed from the list.
 * 
 * 
 */
private class ArrayFilter extends Filter {


private final Object lock = new Object();
private ArrayList<TUser> mOriginalValues;
//protected final LayoutInflater mInflater;

//mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    @Override
    protected FilterResults performFiltering(CharSequence prefix) {
        FilterResults results = new FilterResults();


        if (mOriginalValues == null) {
            synchronized (lock) {
                mOriginalValues = new ArrayList<TUser>(mActivitiesList);
            }
        }

        if (prefix == null || prefix.length() == 0) {
            synchronized (lock) {
                ArrayList<TUser> list = new ArrayList<TUser>(mOriginalValues);
                results.values = list;
                results.count = list.size();
            }
        } else {
            final String prefixString = prefix.toString().toLowerCase();

            ArrayList<TUser> values = mOriginalValues;
            int count = values.size();

            ArrayList<TUser> newValues = new ArrayList<TUser>(count);

            for (int i = 0; i < count; i++) {
                TUser item = values.get(i);

                String[] words = (item.FName.toLowerCase() + " " + item.Name.toLowerCase()).split(" ");  //item.label.toString().toLowerCase().split(" ");
                int wordCount = words.length;

                for (int k = 0; k < wordCount; k++) {
                    final String word = words[k];

                    if (word.contains(prefixString)) {
                        newValues.add(item);
                        break;
                    }
                }
            }

            results.values = newValues;
            results.count = newValues.size();
        }

        return results;
    }



    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        //noinspection unchecked
        mActivitiesList = (List<TUser>) results.values;
        if (results.count > 0) {
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }
    }
}

}

そして、私の主な活動には、このコードがあります。

     etPeopleSearch.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
            /*ListView av = listView;
            PeopleAdapter filterAdapter = (PeopleAdapter)av.getAdapter();
            filterAdapter.getFilter().filter(s.toString());*/

            //PeopleAdapter af = (PeopleAdapter)listView.getAdapter();

            PeopleAdapt.getFilter().filter(s.toString());

        }
    });

すべてがエラーなしで進みますが、フィルタリングが機能しません...誰か助けてくれますか?

4

1 に答える 1

0

この解決策を試してください:

public void onTextChanged(CharSequence s, int start, int before, int count)
{
    PeopleAdapt.getFilter().filter(s); //Filter from my adapter
    PeopleAdapt.notifyDataSetChanged(); //Update my view
}
于 2012-08-31T09:23:02.970 に答える