SearchView In ListView having a custom Adapter
Try this way,hope this will help you...
activity_main.xml
<SearchView
android:id="@+id/searchView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements SearchView.OnQueryTextListener
{
private SearchView mSearchView;
private ListView mListView;
private ArrayList<Employee> employeeArrayList;
private EmployeeAdapter employeeAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSearchView=(SearchView) findViewById(R.id.searchView1);
mListView=(ListView) findViewById(R.id.listView1);
employeeArrayList=new ArrayList<Employee>();
employeeArrayList.add(new Employee("ABC", 24));
employeeArrayList.add(new Employee("ACB", 24));
employeeArrayList.add(new Employee("BVF", 28));
employeeArrayList.add(new Employee("BRT", 28));
employeeArrayList.add(new Employee("ANM", 23));
employeeAdapter=new EmployeeAdapter(MainActivity.this, employeeArrayList);
mListView.setAdapter(employeeAdapter);
mListView.setTextFilterEnabled(true);
setupSearchView();
}
private void setupSearchView()
{
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
}
@Override
public boolean onQueryTextChange(String newText)
{
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
} else {
mListView.setFilterText(newText);
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
return false;
}
}
EmployeeAdapter.java
public class EmployeeAdapter extends BaseAdapter implements Filterable {
public Context context;
public ArrayList<Employee> employeeArrayList;
public ArrayList<Employee> orig;
public EmployeeAdapter(Context context, ArrayList<Employee> employeeArrayList) {
super();
this.context = context;
this.employeeArrayList = employeeArrayList;
}
public class EmployeeHolder
{
TextView name;
TextView age;
}
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Employee> results = new ArrayList<Employee>();
if (orig == null)
orig = employeeArrayList;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final Employee g : orig) {
if (g.getName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
employeeArrayList = (ArrayList<Employee>) results.values;
notifyDataSetChanged();
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public int getCount() {
return employeeArrayList.size();
}
@Override
public Object getItem(int position) {
return employeeArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
EmployeeHolder holder;
if(convertView==null)
{
convertView=LayoutInflater.from(context).inflate(R.layout.row, parent, false);
holder=new EmployeeHolder();
holder.name=(TextView) convertView.findViewById(R.id.txtName);
holder.age=(TextView) convertView.findViewById(R.id.txtAge);
convertView.setTag(holder);
}
else
{
holder=(EmployeeHolder) convertView.getTag();
}
holder.name.setText(employeeArrayList.get(position).getName());
holder.age.setText(String.valueOf(employeeArrayList.get(position).getAge()));
return convertView;
}
}
Employee.java
public class Employee {
private String name;
private int age;
public Employee(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
Good answer is there, but still there are some issues(wrong position when onclick, serchview text overlay) as mentioned in comment section, so you can also do this by following way
Reference for more info : searchview listview android
I have tested it and don't find any issue so far, If you find any, please mention in comment section.
Model class: MovieNames.java
public class MovieNames {
private String movieName;
public MovieNames(String movieName) {
this.movieName = movieName;
}
public String getAnimalName() {
return this.movieName;
}
}
2 : listview_item.xml file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
-
ListViewAdapter.java class adapter for listview :
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.ArrayList; import java.util.Locale; public class ListViewAdapter extends BaseAdapter { // Declare Variables Context mContext; LayoutInflater inflater; private ArrayList<MovieNames> arraylist; public ListViewAdapter(Context context ) { mContext = context; inflater = LayoutInflater.from(mContext); this.arraylist = new ArrayList<MovieNames>(); this.arraylist.addAll(MainActivity.movieNamesArrayList); } public class ViewHolder { TextView name; } @Override public int getCount() { return MainActivity.movieNamesArrayList.size(); } @Override public MovieNames getItem(int position) { return MainActivity.movieNamesArrayList.get(position); } @Override public long getItemId(int position) { return position; } public View getView(final int position, View view, ViewGroup parent) { final ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = inflater.inflate(R.layout.listview_item, null); // Locate the TextViews in listview_item.xml holder.name = (TextView) view.findViewById(R.id.name); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } // Set the results into TextViews holder.name.setText(MainActivity.movieNamesArrayList.get(position).getAnimalName()); return view; } // Filter Class public void filter(String charText) { charText = charText.toLowerCase(Locale.getDefault()); MainActivity.movieNamesArrayList.clear(); if (charText.length() == 0) { MainActivity.movieNamesArrayList.addAll(arraylist); } else { for (MovieNames wp : arraylist) { if (wp.getAnimalName().toLowerCase(Locale.getDefault()).contains(charText)) { MainActivity.movieNamesArrayList.add(wp); } } } notifyDataSetChanged(); } }
-
MainActivity.java class :
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SearchView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener { // Declare Variables private ListView list; private ListViewAdapter adapter; private SearchView editsearch; private String[] moviewList; public static ArrayList<MovieNames> movieNamesArrayList = new ArrayList<MovieNames>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Generate sample data moviewList = new String[]{"Xmen", "Titanic", "Captain America", "Iron man", "Rocky", "Transporter", "Lord of the rings", "The jungle book", "Tarzan","Cars","Shreck"}; // Locate the ListView in listview_main.xml list = (ListView) findViewById(R.id.listview); movieNamesArrayList = new ArrayList<>(); for (int i = 0; i < moviewList.length; i++) { MovieNames movieNames = new MovieNames(moviewList[i]); // Binds all strings into an array movieNamesArrayList.add(movieNames); } // Pass results to ListViewAdapter Class adapter = new ListViewAdapter(this); // Binds the Adapter to the ListView list.setAdapter(adapter); // Locate the EditText in listview_main.xml editsearch = (SearchView) findViewById(R.id.search); editsearch.setOnQueryTextListener(this); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, movieNamesArrayList.get(position).getAnimalName(), Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { String text = newText; adapter.filter(text); return false; } }