Android通过BaseAdapter筛选ListView [英] Android Filter ListView by BaseAdapter

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

问题描述

我试图通过下面的代码编写简单的过滤ListView.但这不能正常工作

i'm try to write simple filtering ListView by this below code. but that does not work correctly

在此代码中,我将ContactListStructure类结构用作:

in this code i'm using ContactListStructure class structure as:

public class ContactListStructure implements Serializable {
    public Long     id;
    public String   name;
    public String   mobile;
    public Bitmap   photo;
    public Boolean  checked;
    ...
}

,我将此类作为带有电话联系人的ArrayList进行填充.填充后,我将这个列表设置为ListView没问题,然后我尝试通过以下方式填充此列表:

and i'm fill this class as an ArrayList with phone contacts. after fill that i'm set this list into ListView without any problem,then i'm try to fillter this list by:

/* private ArrayList<ContactListStructure>  contact_item; */

contactsAdapter = new ContactsAdapter ( G.contact_item , ActivityContactList.this);
lstContent.setAdapter ( contactsAdapter );

search_contacts.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        contactsAdapter.getFilter().filter(s.toString());
    }

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

    @Override
    public void afterTextChanged(Editable s) {
    }
});

发布更新 search_contactsEditText,在键入该ListView之后,不要过滤并不要进行任何更改.我的BaseAdapter:

POST UPDATE search_contacts is an EditText and after type into that ListView dont filter and dont any change. my BaseAdapter :

public class ContactsAdapter extends BaseAdapter  implements Filterable {
    private Context mCtx=null;
    private ArrayList<ContactListStructure> mData=null;
    private ArrayList<ContactListStructure> arraylist;
    private List<ContactListStructure> worldpopulationlist = null;

    private ItemFilter mFilter = new ItemFilter();
    static int i=0;
    public ContactsAdapter (ArrayList<ContactListStructure> contact_item, Context ctx) {
        mData=contact_item;
        mCtx=ctx;
        this.worldpopulationlist = contact_item;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public ContactListStructure getItem(int position) {
            return worldpopulationlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView==null) {
            LayoutInflater inflator=((Activity)mCtx).getLayoutInflater();
            convertView=inflator.inflate(R.layout.send_sms,null);

        }
        CheckBox  chk_name_mobile   = (CheckBox)convertView.findViewById(R.id.chk_name_mobile);
        ImageView photo             = (ImageView)convertView.findViewById(R.id.photo);

        String name=mData.get(position).name;
        chk_name_mobile.setText(name);
        if( mData.get(position).photo == null )
            photo.setImageDrawable( G.context.getResources().getDrawable(R.drawable.user) );
        else
            photo.setImageBitmap(mData.get(position).photo);
        return convertView;
    }

    @Override
    public Filter getFilter () {
        return mFilter;
    }

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

            final ArrayList<ContactListStructure> list = mData;

            int count = list.size();
            final ArrayList<ContactListStructure> nlist = new ArrayList<ContactListStructure>(count);

            String filterableString ;

            for (ContactListStructure wp : worldpopulationlist)
            {
                if (wp.getName().contains(filterString))
                {
                    //Log.e ("wp: ", String.valueOf ( wp ) );
                    ContactListStructure item = new ContactListStructure();
                    item.id = wp.id;
                    item.name = wp.name;
                    item.mobile = wp.mobile;
                    item.photo = wp.photo;
                    nlist.add(item);
                }
            }

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

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            arraylist = (ArrayList<ContactListStructure>) results.values;
            notifyDataSetChanged();
        }
    }
}

更新后:

在评论后,我的notifyDataSetChanged无法正常工作.输入编辑文本后,可以找到,但列表视图不刷新.

after help in comments my notifyDataSetChanged not working. after type into edit text thats can be find but listview dont refresh.

推荐答案

我认为您忘记了将结果返回的数据添加到您的mData:

I think you forgot to add the data returned from result to your mData :

    protected void publishResults(CharSequence constraint, FilterResults results) {
        arraylist = (ArrayList<ContactListStructure>) results.values;
        mData.clear();
        for(int i = 0, i < arraylist.size(); i++)
           mData.add(arraylist.get(i));
        notifyDataSetChanged();
    }
}

这篇关于Android通过BaseAdapter筛选ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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