I want to implement a searchView to check the content of my ListView which exists in a Fragment page.
Before creating the searchView, everything worked fine ( I mean, the ListView existes). But now that I created my searchView, my app crashes.
Here's the code:
public class OngletCours extends Fragment implements Filterable{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.ongletcours, container, false);
SearchView sv = (SearchView)rootView.findViewById(R.id.SVRechercher);
List<Cours> listeCours;
ListView l1= (ListView)rootView.findViewById(R.id.ListCours);
DatabaseHelper dbhelper = new DatabaseHelper(getContext());
ArrayList<String> arrayList ;
listeCours= dbhelper.getAllCours();
if (!listeCours.isEmpty()){
String item;
String[] cours = {""};
arrayList=new ArrayList<>(Arrays.asList(cours));
ArrayAdapter adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1, arrayList);
l1.setAdapter(adapter);
for(int i = 0; i < listeCours.size(); i++) {
item = listeCours.get(i).getCours();
arrayList.add(item);
adapter.notifyDataSetChanged();
Collections.sort(arrayList, String.CASE_INSENSITIVE_ORDER);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
return false;
}
@Override
public boolean onQueryTextChange(String text) {
return false;
}
});
}
} else {
Toast t = Toast.makeText(getActivity(),"Error",Toast.LENGTH_LONG);
t.show();
}
return rootView;
}
}
I've already read a few documents but I can't find the solution.
Here's my error :
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.ClassCastException: android.widget.SearchView cannot be cast to android.support.v7.widget.SearchView at com.example.dasilvadd.students.OngletCours.onCreateView(OngletCours.java:33)