0

In Google Now's 'reminder' feature, you can set a date after being prompted with a view of a calendar and selecting a date. I noticed that once a date is selected, however, the displayed text of the Spinner is the date selected, however the item is not found in the dropdown list. I would like to to do essentially the same thing for my code.

Reminder's Spinner abilities

adneal
  • 30,484
  • 10
  • 122
  • 151
AlleyOOP
  • 1,536
  • 4
  • 20
  • 43

2 Answers2

0

Create a custom SpinnerAdapter and override Adapter.getView for the default "Thursday, April 24" view and SpinnerAdapter.getDropDownView for the drop-down list. To bind the data to your Spinner, call Spinner.setAdapter.

public class YourAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        // How many items are in the data set represented by this Adapter
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // Get the data item associated with the specified position in the data
        // set.
        return null;
    }

    @Override
    public long getItemId(int position) {
        // Get the row id associated with the specified position in the list
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get a View that displays the data at the specified position in the
        // data set
        return null;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        // Get a View that displays in the drop down popup the data
        // at the specified position in the data set
        return super.getDropDownView(position, convertView, parent);
    }

}

final Spinner spinner = ...;
spinner.setAdapter(new YourAdapter());
adneal
  • 30,484
  • 10
  • 122
  • 151
0

You have to extend an Adapter subclass and then override the getView() method.

public class NavigationSpinnerAdapter extends ArrayAdapter {

public NavigationSpinnerAdapter(Context context, int resource, int textViewResourceId, List objects) {
    super(context, resource, textViewResourceId, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView view = (TextView) super
            .getView(position, convertView, parent);
    view.setText("Thursday, April 24");
    return view;
}
}

Then you use it like this:

final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ArrayList<String> itemList = new ArrayList<String>();
itemList.add("Today");
itemList.add("Tomorrow");
itemList.add("Set Date...");
ArrayAdapter<String> arrayAdapter = new NavigationSpinnerAdapter(this, android.R.layout.simple_spinner_dropdown_item, android.R.id.text1, itemList);
actionBar.setListNavigationCallbacks(arrayAdapter, this);
kurryt
  • 210
  • 4
  • 10