2

I have an array called spinnerItems populated by objects of the type DateSpinnerItem. I am passing it to an adapter to be used by a Spinner.

My DataSpinnerItem objects have three properties: Label, Date1 and Date2.

My question is, is there a way for me to add them to an adapter, but in the options show only the object.Label?

My DataSpinnerItem class:

public class DateSpinnerItem {

public String Label;
public String dateInicioStringValue;
public String dateFinalStringValue;

public DateSpinnerItem(){

}
}

My adapter:

ArrayAdapter<DateSpinnerItem> adapter = new ArrayAdapter<DateSpinnerItem>(this, android.R.layout.simple_spinner_dropdown_item) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            //((TextView) v).setGravity(Gravity.CENTER);
            return v;
        }

        @Override
        public int getCount() {
            return super.getCount();
        }

        public View getDropDownView(int position, View convertView,ViewGroup parent) {

            View v = super.getDropDownView(position, convertView,parent);

            ((TextView) v).setGravity(Gravity.CENTER);

            return v;

        }
    };

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    for (int i=0; i<spinnerItems.length; i++){
        adapter.add(spinnerItems[i]);
    }

    sp.setAdapter(adapter);
    sp.setSelection(adapter.getCount()-1); //display hint

Thanks in advance!

Yurigm
  • 53
  • 8

1 Answers1

1

Found a solution! It was actually a lot simpler than I thought.

Just had to override the toString method from my custom class (DataSpinnerItem) to return the Label:

public class DateSpinnerItem {

public String Label;
public Date dateInicioStringValue;
public Date dateFinalStringValue;

public DateSpinnerItem(){

}

@Override
public String toString() {
    return Label;
}
}

That way, I can just add the DataSpinnerItems to the adapter and thanks to the toString() override it shows the label for me representing the added object!

Yurigm
  • 53
  • 8