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

search option in custom listview

我的应用程序中有一个编辑文本和一个列表视图,我的列表视图显示联系人列表。 我想要带有编辑文本的列表视图过滤器。我在 google 上搜索了很多,找到了一些考试,但 none 对我有用这是我的代码

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

我不知道为什么当我在编辑文本中输入一些东西时过滤器不起作用

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

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 的 class 来执行过滤:

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

声明

private ArrayAdapter<String> xListAdapter;

EditText xSearchParty;

作业

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

TextChanged 侦听器

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


            });

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