java Spinner包含周天数,如何根据设备日期选择默认日期
我在这里尝试创建一个java类来:
- 每天检查设备李>
- 创建包含所有工作日的微调器李>
- 微调器将根据设备/计算机日期选择默认日期李>
- 用户可以从微调器中选择另一天李>
- 用户单击“下一步”按钮转到其他表单李>
- 微调器将在所选日期前将意图发送到该表单李>
它没有很好地完成这个过程,它选择了星期四,即使我将设备日更改为星期天,它仍然选择星期四作为默认选择
代码:
public class InformationCustomerListSelectDay extends LiteActivity implements View.OnClickListener {
TextView txtSelectDayTitle, txtSelectDateTitle;
Spinner spinnerSelectDay;
Toolbar toolbar;
Button btnNext;
private String[] arraySpinner;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_information_customer_list_select_day);
String from = getIntent().getStringExtra("FROM");
if(from.equals("ByDayOfWeek")) {
this.arraySpinner = new String[]{
"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
toolbar = (Toolbar) findViewById(R.id.layout_information_customer_list_details_select_day_toolbar);
txtSelectDayTitle = (TextView) findViewById(R.id.layout_information_customer_list_details_select_day_title);
txtSelectDateTitle = (TextView) findViewById(R.id.layout_information_customer_list_details_select_date_title);
spinnerSelectDay = (Spinner) findViewById(R.id.layout_information_customer_list_details_select_day_spinner);
btnNext = (Button) findViewById(R.id.layout_information_customer_list_details_select_day_btn);
btnNext.setOnClickListener(this);
SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date d = new Date();
Calendar calendar = null;
try {
calendar = Calendar.getInstance();
calendar.setTime(d);
} catch (Exception e) {
e.printStackTrace();
}
String dayOfTheWeek = dayFormat.format(d);
String dateOfTheWeek = dateFormat.format(d);
txtSelectDayTitle.setText(dayOfTheWeek);
txtSelectDateTitle.setText(dateOfTheWeek);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 安卓.R.layout.simple_spinner_dropdown_item, arraySpinner);
spinnerSelectDay.setAdapter(adapter);
int day = calendar.DAY_OF_WEEK;
if(calendar != null ){
if (day == 1) {
spinnerSelectDay.setSelection(6);
}
else if (day == 7) {
spinnerSelectDay.setSelection(5);
}
else if (day == 6) {
spinnerSelectDay.setSelection(4);
}
else if (day == 5) {
spinnerSelectDay.setSelection(3);
}
else if (day == 4) {
spinnerSelectDay.setSelection(2);
}
else if (day == 3) {
spinnerSelectDay.setSelection(1);
}
else if (day == 2) {
spinnerSelectDay.setSelection(0);
}
}
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Day Select");
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.layout_information_customer_list_details_select_day_btn:
Intent intentAllRoutCustomer = new Intent(InformationCustomerListSelectDay.this,InformationCustomerListDetails.class);
String spinnerIntent = spinnerSelectDay.getSelectedItem().toString();
intentAllRoutCustomer.putExtra("FROM", "ByDayOfWeek");
intentAllRoutCustomer.putExtra("VALUE", spinnerIntent);
startActivity(intentAllRoutCustomer);
break;
default:
}
}
}
# 1 楼答案
您可能没有正确使用日历。与此相反:
您可能需要这样做:
# 2 楼答案
请尝试一下这个工作代码
# 3 楼答案
确保您有按升序排列的全天列表
然后使用
sppiner.setSelected(getCurrentDayInt);
使用此方法以字符串形式获取当前日期