如何创建一个在Android下拉列表中显示不公开项目的微调器?

时间:2014-04-18 22:45:23

标签: android datepicker spinner google-now

在Google即时的“提醒”功能中,您可以在系统提示日历并选择日期后设置日期。我注意到,一旦选择了日期,Spinner的显示文本就是所选日期,但是在下拉列表中找不到该项目。我想为我的代码做同样的事情。

Reminder's Spinner abilities

2 个答案:

答案 0 :(得分:0)

为默认的“4月24日星期四”视图创建自定义SpinnerAdapter并覆盖Adapter.getView,并为下拉列表创建SpinnerAdapter.getDropDownView。要将数据绑定到Spinner,请致电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());

答案 1 :(得分:0)

您必须扩展Adapter子类,然后重写getView()方法。

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;
}
}

然后你就这样使用它:

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);