0

I have an edittext and a listview in my application my listview show contact list. I want listview filter with edittext. I searched a lot on google and found some examles but none worked for me here's my code

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

i dont know why filter is not working when i typed somthing in edit text

User_B
  • 17
  • 1
  • 4
  • At this line `adapter=new CustomAdapter(Fragment1.this, tempArrayList);` you have just initialized your `adapter` after filter data on text changed but you haven't set that adapter in your listview and you need to refresh your adapter too. So there is problem. So write two more line `listview.setAdapter(adapter);` `adapter.notifyDatasetChanged();` – Piyush Jan 22 '15 at 10:34
  • @piyush now i added this but when i type something in textbox list gets blank. not a single record have been displayed – User_B Jan 22 '15 at 11:40
  • Yes that means you didn't set your adapter in listview. – Piyush Jan 22 '15 at 11:50
  • For filtration on custom listview you can try this [Answer][1]. [1]: http://stackoverflow.com/questions/14118309/how-to-use-search-functionality-in-custom-list-view-in-android/14119383#14119383 – Hiren Patel Jan 22 '15 at 12:42

2 Answers2

0

You should create your custom Adapter like this:

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

Next you will need to create a class that extends Filter to perform your filtering:

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

And to use this inside your Fragment or Activity you should add a TextWatcher to your EditText and filter accordingly:

txtSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s.toString());
    }
});
Leigh
  • 1,495
  • 2
  • 20
  • 42
0

Declaration

private ArrayAdapter<String> xListAdapter;

EditText xSearchParty;

Assignment

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


            });

I hope this code might solve your problem

mdsaleem1804
  • 110
  • 14