美好的一天。我有一个课程,应该在字符串中给我当天的名字,但不是给我当天的名字,而是给我今天前一天的名字。
我一直在检查调试器上发生了什么,显然交换机在当天(当前,它是星期天)工作,并在TextView和ImageView上放置正确的文本和图像。但是一旦调试器完成,显示的图像和文本就是星期六的那些。
这是我的班级:
private void checkWeekday() {
String week_day;
Locale loc = new Locale("es","MX");
SimpleDateFormat day_format = new SimpleDateFormat("EEEE", loc);
Calendar calendar = Calendar.getInstance();
week_day = day_format.format(calendar.getTime());
week_day = week_day.toLowerCase();
switch (week_day) {
case "domingo":
textView_weekday_name.setText(sunday);
imageView_diets_weekday.setImageDrawable(drawable_sunday);
case "lunes":
textView_weekday_name.setText(monday);
imageView_diets_weekday.setImageDrawable(drawable_monday);
case "marter":
textView_weekday_name.setText(tuesday);
imageView_diets_weekday.setImageDrawable(drawable_tuesday);
case "miércoles":
textView_weekday_name.setText(wednesday);
imageView_diets_weekday.setImageDrawable(drawable_wednesday);
case "jueves":
textView_weekday_name.setText(thursday);
imageView_diets_weekday.setImageDrawable(drawable_thursday);
case "viernes":
textView_weekday_name.setText(friday);
imageView_diets_weekday.setImageDrawable(drawable_friday);
case "sábado":
textView_weekday_name.setText(saturday);
imageView_diets_weekday.setImageDrawable(drawable_saturday);
}
}
需要注意的事项:此方法是在片段内的@AfterViews方法上调用的。
我已经尝试将日历位用于我的主@AfterViews方法,但它仍然获得前一天的名称。
我怀疑我正试图将这一天的名字写在错误的地方,但无论我把它放在哪里,它仍会在星期六给我。
作为参考,这是我的@AfterViews方法的外观:
@AfterViews
protected void init() {
checkWeekday();
boolean logged = Prefs.getBoolean(user_logged, false);
boolean subbed = Prefs.getBoolean(user_subscribed, false);
String email = Prefs.getString(user_mail, "");
mixpanel = MixpanelAPI.getInstance(getContext(), mixpanel_token);
setHasOptionsMenu(true);
if (logged && subbed) {
List<UserMeals> meals = new RushSearch().find(UserMeals.class);
if(meals.size() != 0) {
setDataFromLocal();
onClickHandler();
} else {
try {
checkIfNewPlan(email, "fetch");
} catch (JSONException e) {
e.printStackTrace();
}
onClickHandler();
}
} else {
emptyLayout("nosub");
}
}
感谢。
修改
我在交换机的每个案例中都缺少 break 。对此感到尴尬!非常感谢你!
答案 0 :(得分:1)
要使用日历实例获取当前日期,请使用以下方法:
public static String getCurrentDate(){
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return (month+1) +"/"+ day + "/" + year;
}
由于日历月是从0开始的,意味着1月实际上是0而不是1,你需要加1!
<强>更新强>
我刚刚查看了你的switch语句,它在case语句中没有中断?
switch (week_day) {
case "domingo":
textView_weekday_name.setText(sunday);
imageView_diets_weekday.setImageDrawable(drawable_sunday);
break;
case "lunes":
textView_weekday_name.setText(monday);
imageView_diets_weekday.setImageDrawable(drawable_monday);
break;
case "marter":
textView_weekday_name.setText(tuesday);
imageView_diets_weekday.setImageDrawable(drawable_tuesday);
break;
case "miércoles":
textView_weekday_name.setText(wednesday);
imageView_diets_weekday.setImageDrawable(drawable_wednesday);
break;
case "jueves":
textView_weekday_name.setText(thursday);
imageView_diets_weekday.setImageDrawable(drawable_thursday);
break;
case "viernes":
textView_weekday_name.setText(friday);
imageView_diets_weekday.setImageDrawable(drawable_friday);
break;
case "sábado":
textView_weekday_name.setText(saturday);
imageView_diets_weekday.setImageDrawable(drawable_saturday);
break;
}
我感觉这是问题!