1

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)

Sufian
  • 6,405
  • 16
  • 66
  • 120
David Silva
  • 67
  • 1
  • 11
  • You are using 2 SearchViews from different packages .Use either one of them `android.support.v7.widget.SearchView` or `android.widget.SearchView` .Check your imports. Import should look like this http://stackoverflow.com/a/24522846/3111083. Get rid of all v7 Searchview from import – Sunil Sunny Feb 20 '17 at 09:06
  • Possible duplicate of [Can't cast SearchView in Fragment (AppCompat)](http://stackoverflow.com/questions/22150885/cant-cast-searchview-in-fragment-appcompat) – Sufian Feb 20 '17 at 10:46

1 Answers1

0

You must changer this:

import android.widget.SearchView;

to this:

import android.support.v7.widget.SearchView;

in MainActivity.java.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42