This is an example of using Filterable with a SearchView to filter the data of a ListView adapter.
-
-
Save Abdallah-Abdelazim/e62fc23dae13c1dd8de43fe6c7ac55a2 to your computer and use it in GitHub Desktop.
Filtering ListView using Filterable interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity"> | |
<SearchView | |
android:id="@+id/search_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:iconifiedByDefault="false" | |
android:padding="2dp" | |
android:queryHint="Search...." /> | |
<ListView | |
android:id="@+id/lv" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_below="@+id/search_view"></ListView> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.almabay.filterlistdemo; | |
/** | |
* Created by deepakr on 3/29/2016. | |
*/ | |
public class Bean { | |
String name, age; | |
public Bean(String name, String age) { | |
this.name = name; | |
this.age = age; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getAge() { | |
return age; | |
} | |
public void setAge(String age) { | |
this.age = age; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.almabay.filterlistdemo; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.ListView; | |
import android.widget.SearchView; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener { | |
private ListView lv; | |
private SearchView sv; | |
private List<Bean> beanList; | |
MyAdapter adapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
sv = (SearchView) findViewById(R.id.search_view); | |
lv = (ListView) findViewById(R.id.lv); | |
String[] name = {"Deepak", "Ravi", "Raj", "Rakesh", "Mandeep", "Sishu"}; | |
String[] age = {"20", "30", "25", "45", "50", "35"}; | |
beanList = new ArrayList<Bean>(); | |
for (int i = 0; i < name.length; i++) { | |
Bean bean = new Bean(name[i], age[i]); | |
beanList.add(bean); | |
} | |
for (int i = 0; i < beanList.size(); i++) { | |
Bean bean = beanList.get(i); | |
String n = bean.getName(); | |
Log.e("Name", n); | |
} | |
adapter = new MyAdapter(getApplicationContext(), beanList); | |
lv.setAdapter(adapter); | |
sv.setOnQueryTextListener(this); | |
} | |
@Override | |
public boolean onQueryTextSubmit(String s) { | |
adapter.getFilter().filter(s); | |
return false; | |
} | |
@Override | |
public boolean onQueryTextChange(String s) { | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.almabay.filterlistdemo; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Adapter; | |
import android.widget.BaseAdapter; | |
import android.widget.Filter; | |
import android.widget.Filterable; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by deepakr on 3/29/2016. | |
*/ | |
public class MyAdapter extends BaseAdapter implements Filterable { | |
private Context context; | |
private List<Bean> beanList; | |
private LayoutInflater inflater; | |
List<Bean> mStringFilterList; | |
ValueFilter valueFilter; | |
public MyAdapter(Context context, List<Bean> beanList) { | |
this.context = context; | |
this.beanList = beanList; | |
mStringFilterList = beanList; | |
} | |
@Override | |
public int getCount() { | |
return beanList.size(); | |
} | |
@Override | |
public Object getItem(int i) { | |
return beanList.get(i); | |
} | |
@Override | |
public long getItemId(int i) { | |
return i; | |
} | |
@Override | |
public View getView(int i, View view, ViewGroup viewGroup) { | |
if (inflater == null) { | |
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
} | |
if (view == null) { | |
view = inflater.inflate(R.layout.single_row, null); | |
} | |
TextView txtName = (TextView) view.findViewById(R.id.name); | |
TextView txtAge = (TextView) view.findViewById(R.id.age); | |
Bean bean = beanList.get(i); | |
String name = bean.getName(); | |
String age = bean.getAge(); | |
txtName.setText(name); | |
txtAge.setText(age); | |
return view; | |
} | |
@Override | |
public Filter getFilter() { | |
if (valueFilter == null) { | |
valueFilter = new ValueFilter(); | |
} | |
return valueFilter; | |
} | |
private class ValueFilter extends Filter { | |
@Override | |
protected FilterResults performFiltering(CharSequence constraint) { | |
FilterResults results = new FilterResults(); | |
if (constraint != null && constraint.length() > 0) { | |
ArrayList<Bean> filterList = new ArrayList<Bean>(); | |
for (int i = 0; i < mStringFilterList.size(); i++) { | |
if ((mStringFilterList.get(i).getName().toUpperCase()) | |
.contains(constraint.toString().toUpperCase())) { | |
Bean bean = new Bean(mStringFilterList.get(i) | |
.getName(), mStringFilterList.get(i) | |
.getAge()); | |
filterList.add(bean); | |
} | |
} | |
results.count = filterList.size(); | |
results.values = filterList; | |
} else { | |
results.count = mStringFilterList.size(); | |
results.values = mStringFilterList; | |
} | |
return results; | |
} | |
@Override | |
protected void publishResults(CharSequence constraint, | |
FilterResults results) { | |
beanList = (ArrayList<Bean>) results.values; | |
notifyDataSetChanged(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="horizontal"> | |
<TextView | |
android:id="@+id/name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:id="@+id/age" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="20dp" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment