Spinner 包含工作日,我如何 select 默认日期取决于设备日期

Spinner contains week days, how can I select the default day depending on the device day

我正在尝试创建一个 java class 到:

  1. 检查设备日期。
  2. 创建一个包含所有工作日的微调器。
  3. Spinner 将 select 默认日期取决于 device/computer 天。
  4. 用户可以 select 另一天从微调器。
  5. 用户单击 "next" 按钮转到另一个表单。
  6. Spinner 将在所选日期之前将意向发送到该表单。

它没有很好地完成这个过程,它是 select 星期四,即使我将设备日期更改为星期日,它仍然 select 星期四作为默认 selection。

代码:

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, android.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:

    }

    }
}

您可能没有正确使用日历。而不是这个:

int day = calendar.DAY_OF_WEEK;

您可能需要这样做:

int day = calendar.get(Calendar.DAY_OF_WEEK);

确保您有按升序排列的全天列表

 public int getCurrentDayInt()
        {
            calendar = Calendar.getInstance();
            dateFormat = new DateFormatSymbols(Locale.getDefault());

            return  calendar.get(calendar.DAY_OF_WEEK);
        }

然后使用sppiner.setSelected(getCurrentDayInt);

使用此方法以字符串形式获取当前日期

 public String getCurrentDay()
    {
        calendar = Calendar.getInstance();
        dateFormat = new DateFormatSymbols(Locale.getDefault());
        intDAY = calendar.get(calendar.DAY_OF_WEEK);
        return  dateFormat.getWeekdays()[intDAY];
    }

请尝试使用此工作代码。

public class InformationCustomerListSelectDay extends AppCompatActivity {

        // Array that defines days of the week.
        String[] weekdays = new String[]{"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

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

            //Get the day of the week for perticular device
            SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
            Date d = new Date();
            String dayOfTheWeek = sdf.format(d);

            // Get the array index from day of the week.
            int defaultPosition = Arrays.asList(weekdays).indexOf(dayOfTheWeek);

            // Apply index to spinner.
            spinnerSelectDay.setSelection(defaultPosition);
        }

    }