在我的适配器类中,我使用了两个全局变量。
public static String prevYearSelected =“”,currentYearSelected =“”;
如果我的方法,基于两个条件,我将值分配给两个变量。
代码:
public static String prevYearSelected = "",currentYearSelected = "";
if (selectedMonth.equalsIgnoreCase("01")) {
if(isCurrentYearSelected == true)
{
currentYearSelected = selectedDate + " " + "Januari" + " " + selectedYear;
}
else
{
prevYearSelected = selectedDate + " " + "Januari" + " " + selectedYear + " " + "-";
}
selectedDateTV.setText(prevYearSelected+currentYearSelected);
}
if (selectedMonth.equalsIgnoreCase("02")) {
if(isCurrentYearSelected == true)
{
currentYearSelected = selectedDate + " " + "Februari" + " " + selectedYear;
}
else
{
prevYearSelected = selectedDate + " " + "Februari" + " " + selectedYear + " " + "-";
}
selectedDateTV.setText(prevYearSelected+currentYearSelected);
}
实际上,根据日历中的年份选择,我将值传递给全局参数。例如,如果我选择了2018年3月5日,那么我将在当前年份中通过该值,如果我选择2017年3月8日,那么我将该值传递给prevYearSelected。
但问题是当我将两个值都设置为textview
时,它显示其中一个值为null ...即如果我选择2017日期,则当前值显示为null,如果我选择2018 date然后显示prev值为null。我真的不明白为什么全局变量返回空值而不是持久化数据。
我真的非常感谢你们的帮助。
谢谢
答案 0 :(得分:0)
试试这个:
public static String prevYearSelected = "",currentYearSelected = "";
if (selectedMonth.equalsIgnoreCase("01")) {
if(isCurrentYearSelected == true)
{
currentYearSelected = selectedDate + " " + "Januari" + " " + selectedYear;
prevYearSelected = selectedDate + " " + "Januari" + " " + (selectedYear - 1) + " " + "-";
}
else
{
prevYearSelected = selectedDate + " " + "Januari" + " " + selectedYear + " " + "-";
currentYearSelected = selectedDate + " " + "Januari" + " " + (selectedYear + 1);
}
selectedDateTV.setText(prevYearSelected+currentYearSelected);
}
if (selectedMonth.equalsIgnoreCase("02")) {
if(isCurrentYearSelected == true)
{
currentYearSelected = selectedDate + " " + "Februari" + " " + selectedYear;
prevYearSelected = selectedDate + " " + "Februari" + " " + (selectedYear - 1) + " " + "-";
}
else
{
prevYearSelected = selectedDate + " " + "Februari" + " " + selectedYear + " " + "-";
currentYearSelected = selectedDate + " " + "Februari" + " " + (selectedYear + 1);
}
selectedDateTV.setText(prevYearSelected+currentYearSelected);
}