Search code examples
javaandroidandroid-spinner

Android topoche searchable spinner cannot cast error


Error Message Inconvertible types; cannot cast 'android.view.View' to 'com.toptoche.searchablespinnerlibrary.SearchableSpinner'


Solution

  • Spinner 1 is assigned to the wrong thing you have it assigned as a View not a spinner Easier way to do this,

    Before your onCreate you do

    Spinner spinnername; 
    

    so then the spinner is available to call throughout the entire activity

    Then under your your onCreate you do

    spinnername=findViewById(R.id.nameofspinnerinxml);
    

    And in your xml you must make your spinner something like this

      <com.toptoche.searchablespinnerlibrary.SearchableSpinner
        android:id="@+id/nameofspinnerinxml"
        style="@android:style/Widget.Holo.Light.Spinner"
        android:layout_width="match_parent"
        android:layout_height="51dp"
        tools:layout_editor_absoluteY="24dp" />
    

    Adding a more complete example for you

    Spinner spinner1;
    
        @TargetApi(Build.VERSION_CODES.N)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_edit_project);
    
            spinner1=findViewById(R.id.spinnerid);
    

    and then in the xml

      <com.toptoche.searchablespinnerlibrary.SearchableSpinner
        android:id="@+id/spinnerid"
        style="@android:style/Widget.Holo.Light.Spinner"
        android:layout_width="match_parent"
        android:layout_height="51dp"
        tools:layout_editor_absoluteY="24dp" />