Search code examples
androidspinnerandroid-arrayadaptersimplecursoradapter

Hiding Spinner Items in Dropdown


I have multiple spinners on one xml layout and each of them start with "...PLEASE SELECT". These are part of my database, not placed in by code. I would like to hide the "...PLEASE SELECT" when the dropdown of any of my spinners get selected.

I have read many questions regarding this topic and they do not work for me because I am using a SimpleCursorAdapter to populate my spinners not an ArrayAdapter. If I was using an ArrayAdapter I could override the getDropDownView() method but because I am using a SimpleCursorAdapter I can't do this. If there are answers regarding my issue by using a SimpleCursorAdapter and removing/hiding the first row of a spinner dropdown, I cannot find them.

How can I remove the first spinner item when the dropdown opens without using an ArrayAdapter?


Solution

  • Instead of Spinner you should use EditText with a hint attribute.

    For example:

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_text_hint"
        android:cursorVisible="false"
        android:inputType="none"
        android:textIsSelectable="true"/>
    

    In Java-code:

    editText.setOnFocusChangeListener(
    
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) return;
    
            // Показываем диалог
            MyDialog dialog = new MyDialog();
            dialog.show(getFragmentManager(), "");
        });
    
    
    public void onItemSelected(String itemName) {
        editText.setText(itemName);
    }
    

    And you should create a DialogFragment class MyDialog which displays a list of items and calls onItemSelected callback.