Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List View Filter Android

I have created a list view in android and I want to add edit text above the list and when the user enter text the list will be filtered according to user input

can anyone tell me please if there is a way to filter the list adapter in android ?

like image 251
Amira Elsayed Ismail Avatar asked Feb 02 '13 16:02

Amira Elsayed Ismail


People also ask

How can use sorting and filter list data in Android?

Create a filter. The "Sort and filter" menu will open. Type in the search bar to find a value or scroll up on the list to see the values in the column. Tap an item to uncheck it and filter it out. The sheet will update automatically.

How is it possible to view a list in Android?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.

How do I change the filter adapter on my Android?

In its simplest form, you just create an array adapter passing it a list of objects (that have a proper toString() method). Then you type some characters to the textbox and by default it will filter the results searching in the beginning of the backing object's toString() result.


2 Answers

Add an EditText on top of your listview in its .xml layout file. And in your activity/fragment..

lv = (ListView) findViewById(R.id.list_view);     inputSearch = (EditText) findViewById(R.id.inputSearch);  // Adding items to listview adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name,    products); lv.setAdapter(adapter);        inputSearch.addTextChangedListener(new TextWatcher() {      @Override     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {         // When user changed the Text         MainActivity.this.adapter.getFilter().filter(cs);     }      @Override     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }      @Override     public void afterTextChanged(Editable arg0) {} }); 

The basic here is to add an OnTextChangeListener to your edit text and inside its callback method apply filter to your listview's adapter.

EDIT

To get filter to your custom BaseAdapter you"ll need to implement Filterable interface.

class CustomAdapter extends BaseAdapter implements Filterable {      public View getView(){     ...     }     public Integer getCount()     {     ...     }      @Override     public Filter getFilter() {          Filter filter = new Filter() {              @SuppressWarnings("unchecked")             @Override             protected void publishResults(CharSequence constraint, FilterResults results) {                  arrayListNames = (List<String>) results.values;                 notifyDataSetChanged();             }              @Override             protected FilterResults performFiltering(CharSequence constraint) {                  FilterResults results = new FilterResults();                 ArrayList<String> FilteredArrayNames = new ArrayList<String>();                  // perform your search here using the searchConstraint String.                  constraint = constraint.toString().toLowerCase();                 for (int i = 0; i < mDatabaseOfNames.size(); i++) {                     String dataNames = mDatabaseOfNames.get(i);                     if (dataNames.toLowerCase().startsWith(constraint.toString()))  {                         FilteredArrayNames.add(dataNames);                     }                 }                  results.count = FilteredArrayNames.size();                 results.values = FilteredArrayNames;                 Log.e("VALUES", results.values.toString());                  return results;             }         };          return filter;     } } 

Inside performFiltering() you need to do actual comparison of the search query to values in your database. It will pass its result to publishResults() method.

like image 181
Purush Pawar Avatar answered Sep 23 '22 18:09

Purush Pawar


Implement your adapter Filterable:

public class vJournalAdapter extends ArrayAdapter<JournalModel> implements Filterable{ private ArrayList<JournalModel> items; private Context mContext; .... 

then create your Filter class:

private class JournalFilter extends Filter{      @Override     protected FilterResults performFiltering(CharSequence constraint) {         FilterResults result = new FilterResults();         List<JournalModel> allJournals = getAllJournals();         if(constraint == null || constraint.length() == 0){              result.values = allJournals;             result.count = allJournals.size();         }else{             ArrayList<JournalModel> filteredList = new ArrayList<JournalModel>();             for(JournalModel j: allJournals){                 if(j.source.title.contains(constraint))                     filteredList.add(j);             }             result.values = filteredList;             result.count = filteredList.size();         }          return result;     }     @SuppressWarnings("unchecked")     @Override     protected void publishResults(CharSequence constraint, FilterResults results) {         if (results.count == 0) {             notifyDataSetInvalidated();         } else {             items = (ArrayList<JournalModel>) results.values;             notifyDataSetChanged();         }     }  } 

this way, your adapter is Filterable, you can pass filter item to adapter's filter and do the work. I hope this will be helpful.

like image 45
M.Kouchi Avatar answered Sep 22 '22 18:09

M.Kouchi