如何创建Spinner以显示当前和下30个日期

时间:2014-02-03 12:05:52

标签: java android date android-spinner custom-datetimepicker

如何创建日期微调器,它将EditText中的当前日期显示为Spinner中的默认日期和未来日期(例如,接下来的30天)

我在很多应用程序中使用了日期选择器,所以我熟悉日期选择器对话框,但对日期微调器没有任何了解。

注意
请不要告诉我如何将EditText设置为Spinner

已编辑:1 @erakitin

推荐
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_date);

    Spinner spinnerDateIn = (Spinner) findViewById(R.id.spinnerDateIn);
    Spinner spinnerDateOut = (Spinner) findViewById(R.id.spinnerDateOut);       

    spinnerDateIn.setAdapter(new CalendarSpinnerAdapter(SpinnerDateActivity.this, 30));         

}

已编辑:2 在新更新中@erakitin推荐

CalendarSpinnerAdapter mSpinnerDateInAdapter = new CalendarSpinnerAdapter(SpinnerDateActivity.this, 30);
        spinnerDate.setAdapter(mSpinnerDateInAdapter);

        spinnerDate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                spinnerDate.setSelection(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

现在,尝试在String中获取所选Item的值,但获取:java.util.GregorianCalendar,我在哪里做错了?

strDate = spinnerDate.getSelectedItem().toString();

2 个答案:

答案 0 :(得分:5)

以下Spinner有一个简单的适配器示例。

public class CalendarSpinnerAdapter extends BaseAdapter {

    private SimpleDateFormat mDateFormat = new SimpleDateFormat("d MMM yyyy");

    private LayoutInflater mInflater;
    private Calendar mCalendar;
    private int mDayCount;
    private int mLastRequestedDay = 0;

    public CalendarSpinnerAdapter(Context context, int dayCount) {
        mInflater = LayoutInflater.from(context);
        mDayCount = dayCount;
        mCalendar = Calendar.getInstance();
    }

    @Override
    public int getCount() {
        return mDayCount;
    }

    @Override
    public Calendar getItem(int position) {
        mCalendar.add(Calendar.DAY_OF_YEAR, position - mLastRequestedDay);
        mLastRequestedDay = position;
        return mCalendar;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
        }

        Calendar item = getItem(position);
        ((TextView) convertView).setText(mDateFormat.format(item.getTimeInMillis()));

        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getView(position, convertView, parent);
    }
}

如何将其用于显示当前日期和接下来的29天:

mDateSpinner.setAdapter(new CalendarSpinnerAdapter(getActivity(), 30));

<强> UPD:

我们应该在adapter的构造函数中添加一个参数来设置开始日期:

public CalendarSpinnerAdapter(Context context, Calendar startDate, int dayCount) {
    mInflater = LayoutInflater.from(context);
    mDayCount = dayCount;
    mCalendar = Calendar.getInstance();
    mCalendar.setTimeInMillis(startDate.getTimeInMillis());
}

然后为spinnerDateIn添加监听器,我们可以在其中初始化第二个微调器:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_date);

    Spinner spinnerDateIn = (Spinner) findViewById(R.id.spinnerDateIn);
    final Spinner spinnerDateOut = (Spinner) findViewById(R.id.spinnerDateOut);

    mSpinnerDateInAdapter = new CalendarSpinnerAdapter(SpinnerDateActivity.this, Calendar.getInstance(), 30);
    spinnerDateIn.setAdapter(mSpinnerDateInAdapter);

    spinnerDateIn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Calendar dateIn = Calendar.getInstance();
            dateIn.setTimeInMillis(mSpinnerDateInAdapter.getItem(position).getTimeInMillis());
            dateIn.add(Calendar.DAY_OF_YEAR, 1); // add one day
            mSpinnerDateOutAdapter = new CalendarSpinnerAdapter(SpinnerDateActivity.this, dateIn, 30);
            spinnerDateOut.setAdapter(mSpinnerDateOutAdapter);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

我没有尝试过此代码,但应该可以使用。

<强> UPD2:

spinnerDate.setSelection(position)返回Calendar类实例。如果您希望将所选日期设为String,则应格式化。尝试使用以下方法:

private String getSelectedDateAsString(Spinner dateSpinner) {
    Calendar selectedDate = (Calendar) dateSpinner.getSelectedItem();
    return new SimpleDateFormat("d MMM yyyy").format(selectedDate.getTimeInMillis());
}

答案 1 :(得分:0)

Spinner使用适配器。如果你想在你的微调器中显示从今天到月底你可以做的日期。让我们创建一个模型类,可以使用它来提供适配器,

  public class MyDateInterval {
    public String mDateString;
    public Date mDate;

    public MyDateInterval(String dateString, Date date) {
        mDateString = dateString;
        mDate = date;
    }

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

现在我们定义一个ArrayList<MyDateInterval>,我们将填写您要显示的日期:

ArrayList<MyDateInterval> items = new ArrayList<>();

以示例格式检索结束日期:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, 6);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
// for the sake of the example 31 Jul 2015
Date endDate = c.getTime();
Log.e(getClass().getSimpleName(), dateFormat.format(endDate));

现在我们计算了从今天到结束日期的间隔

Calendar todayCalendar = Calendar.getInstance();
while(endDate.after(todayCalendar.getTime())) {
    items.add(new MyDateInterval(dateFormat.format(todayCalendar.getTime()), todayCalendar.getTime());
    todayCalendar.add(Calendar.DAY_OF_MONTH, 1);
 }

现在我们所有的碎片。您必须为Spinner创建适配器。

ArrayAdapter<MyDateInterval> dataAdapter = new ArrayAdapter<>(this,
                            android.R.layout.simple_spinner_item, items);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

这应该是全部。