有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spinner包含周天数,如何根据设备日期选择默认日期

我在这里尝试创建一个java类来:

  1. 每天检查设备
  2. 创建包含所有工作日的微调器
  3. 微调器将根据设备/计算机日期选择默认日期
  4. 用户可以从微调器中选择另一天
  5. 用户单击“下一步”按钮转到其他表单
  6. 微调器将在所选日期前将意图发送到该表单

它没有很好地完成这个过程,它选择了星期四,即使我将设备日更改为星期天,它仍然选择星期四作为默认选择

代码:

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:

    }

    }
}

共 (3) 个答案

  1. # 1 楼答案

    您可能没有正确使用日历。与此相反:

    int day = calendar.DAY_OF_WEEK;
    

    您可能需要这样做:

    int day = calendar.get(Calendar.DAY_OF_WEEK);
    
  2. # 2 楼答案

    请尝试一下这个工作代码

    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);
            }
    
        }
    
  3. # 3 楼答案

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

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