自定义列表视图中的搜索选项

时间:2015-01-22 10:20:05

标签: android listview

我的应用程序中有一个edittext和listview我的listview显示联系人列表。     我想要使​​用edittext的listview过滤器。我在谷歌搜索了很多,发现了一些考试,但没有一个在我这里工作,我的代码

my custom adapter:

public  class CustomAdapter extends BaseAdapter 
{


    Fragment frgmnt;
    ArrayList<HashMap<String, ArrayList>> contactarr;

    LayoutInflater inflater;   

    public CustomAdapter(Fragment1 frgmnt,  ArrayList<HashMap<String, ArrayList>> contactarr) 
    {
        //
        originalData=contactarr;
        filteredData=contactarr;


        this.frgmnt=frgmnt;
        this.contactarr=contactarr;             
        inflater= (LayoutInflater)getActivity().getSystemServic(Context.LAYOUT_INFLATER_SERVICE);
        this.contactarrsrch=new ArrayList<HashMap<String, ArrayList>>();
        this.contactarrsrch.addAll(contactarr);
    }
    @Override
    public int getCount() 
    {           
        return filteredData.size();
    }

    @Override
    public Object getItem(int position)
    {
         return filteredData.get(position);
        //return null;
    }

    @Override
    public long getItemId(int position)
    {
        // TODO Auto-generated method stub
        return position;
    }   

    @Override
    public View getView(int position, View view, ViewGroup parent) 
    {
        if(view==null)
        {
            view=inflater.inflate(R.layout.list_data,null);
        }
        LinearLayout ll_row =(LinearLayout) view.findViewById(R.id.ll_row);
        TextView txtContact = (TextView) view.findViewById(R.id.contact_name);

        HashMap<String, ArrayList> map=contactarr.get(position);            
        ArrayList name =map.get("Name_");
        txtContact.setText(name.get(0).toString());

        return view;
    }
}   

this is in onCrate():

InputSearch.addTextChangedListener(new TextWatcher() 
    {                   
        final ArrayList<HashMap<String,ArrayList>> tempArrayList =new ArrayList<HashMap<String,ArrayList>>(contactarr1);

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) 
        {   
        String text = InputSearch.getText().toString().toLowerCase(Locale.getDefault());
        //adapter.getFilter(s.toString());  
        int textlength = s.length();
        for (HashMap<String, ArrayList> wp : tempArrayList)
        {

                if(textlength<=wp.get("Name_").toString().length())
                {
                if (( wp.get("Name_").toString()).toLowerCase().contains(s.toString().toLowerCase()))
                {       
                    tempArrayList.add(wp);          
                }
                }
        }
        adapter=new CustomAdapter(Fragment1.this, tempArrayList);
        }   

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

我不知道为什么当我在编辑文本中输入某些内容时过滤器无法正常工作

2 个答案:

答案 0 :(得分:0)

您应该像这样创建自定义适配器:

public class CompanyListAdapter extends BaseAdapter implements Filterable {

    private List<Company> companies;
    private List<Company> filteredCompanies;
    private CompanyFilter mFilter = new CompanyFilter();

    private Context context;

    public CompanyListAdapter(Context context, ArrayList<Company> products) {
        super(context, R.layout.company_list_item, products);
        this.context = context;
        this.products = products;
        this.filteredCompanies = products;
    }

    @Override
    public int getCount() {
        return filteredCompanies == null 0 : filteredCompanies.size();
    }

    @Override
    public Company getItem(int position) {
        return filteredCompanies.get(position);
    }

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

接下来,您需要创建一个扩展Filter的类来执行过滤:

private class CompanyFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();
        FilterResults results = new FilterResults();

        final List<Company> list = companies;

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

        Company filterableCompany;

        for (int i = 0; i < count; i++) {
            filterableCompany = list.get(i);
            if (filterableCompany.getName().toLowerCase().contains(filterString)) {
                nlist.add(filterableCompany);
            }
        }

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

        return results;
    }

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

要在FragmentActivity中使用此功能,您应该在TextWatcher添加EditText并进行相应过滤:

txtSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s.toString());
    }
});

答案 1 :(得分:0)

声明

private ArrayAdapter<String> xListAdapter;

EditText xSearchParty;

分配

xLvParty = (ListView) findViewById(R.id.lvparty);
xSearchParty = (EditText) findViewById(R.id.inputSearch);

TextChanged Listener

xSearchParty.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2,
                        int arg3) {
                    // When user changed the Text
                    FILENAME.this.xListAdapter.getFilter().filter(cs);
                }


            });

我希望这段代码可以解决您的问题