Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner with an empty default selection

I have a Spinner which gets populate using a SimpleCursorAdapter. My cursor has some values, but i need the Spinner to show an empty option by default.

I don't want to use ArrayAdapter<String>, or CursorWrapper in this app, for some reason.

There should be a simpler way to show an empty option in the Spinner by default.

like image 511
AAP Avatar asked Jan 28 '12 19:01

AAP


1 Answers

You can simply hide the unwanted view in the spinner adapter (getDropDownView ) :

In my sample code, defaultposition is the position to hide (like a "Select value" position)

public class SpinnerOptionAdapter extends ArrayAdapter<optionsInfos> {

 ...

   @Override

  public View getDropDownView(int position, View convertView, ViewGroup parent)
  {   // This view starts when we click the spinner.
    View row = convertView;
    if(row == null)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        row = inflater.inflate(R.layout.product_tab_produit_spinner_layout, parent, false);
    }

    ...

    optionsInfos item = data.get(position);


    if( (item != null) && ( position == defaultposition)) {
        row.setVisibility(View.GONE);
    } else {
        row.setVisibility(View.VISIBLE);
    }

   ....

    return row;
}


 ...
}
like image 61
Ralph Avatar answered Nov 15 '22 17:11

Ralph