- Initialize in first java file
SearchView searchView;
ListView listView;
ArrayList<String> list;
ArrayAdapter<String > adapter;
- Insert This:-
searchView = (SearchView) findViewById(R.id.searchView);
listView = (ListView) findViewById(R.id.lv1);
list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Pineapple");
list.add("Orange");
list.add("Lychee");
list.add("Gavava");
list.add("Peech");
list.add("Melon");
list.add("Watermelon");
list.add("Papaya");
- Set Adapter
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
if(list.contains(s)){
adapter.getFilter().filter(s);
}
else{
Toast.makeText(SearchFilter.this, "Match Not Found", Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
return false;
}
});
0 Comments