A.shmrani A.shmrani - 6 months ago 16
Java Question

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

I'm trying here to create a java class to:


  1. check the device day.

  2. Create a spinner that contains all week days.

  3. Spinner will select the default day depending on the device/computer day.

  4. User can select another day from the spinner.

  5. User click the "next" button to go to another form.

  6. Spinner will send intent by the chosen day to that form.



it's not doing the process well, it is selecting Thursday even when I change the device day to Sunday, it keep selecting Thursday as default selection.

code :

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:

}

}
}

Answer

you are probably using calendar incorrectly. instead of this:

int day = calendar.DAY_OF_WEEK;

you probably need to do this:

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