Android自定义ListView适配器过滤 [英] Android Custom ListView adapter filtering

查看:108
本文介绍了Android自定义ListView适配器过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ListView
中过滤自定义适配器时遇到问题,这是我的适配器代码

I have problem with filterin my custom adapter in my ListView Here is my code of Adapter

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();
        }
    }
}

}

在我的主要活动中,我有这段代码。

And in my main activity i have this code.

     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());

        }
    });

一切都没有错误,但过滤不起作用...有人可以帮助我吗?

Everything goes without error but filtering does not work... can anyone help me?

推荐答案

尝试以下解决方案:

public void onTextChanged(CharSequence s, int start, int before, int count)
{
    PeopleAdapt.getFilter().filter(s); //Filter from my adapter
    PeopleAdapt.notifyDataSetChanged(); //Update my view
}

这篇关于Android自定义ListView适配器过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆