Wraithious Wraithious - 5 months ago 10
Android Question

String value not showing in output.setText

Im trying to show the day name along with the date in my app and the text defined as "text" shows up fine and integer values also show up fine, but it will not show a text variable, this is what I have:

Calendar now = Calendar.getInstance();
TextView output3 = (TextView) findViewById(R.id.output3);

int dayno = now.get(Calendar.DAY_OF_WEEK);
String dayName = new String();// also tried String dayName = ""; with the same results
if (dayno == 1) dayName = "Sun";
if (dayno == 1) dayName = "Mon";
if (dayno == 1) dayName = "Tue";
if (dayno == 1) dayName = "Wed";
if (dayno == 1) dayName = "Thu";
if (dayno == 1) dayName = "Fri";
if (dayno == 1) dayName = "Sat";


also tried it like this:

if (dayno == 1) {dayName = "Sun";}
if (dayno == 1) {dayName = "Mon";}
if (dayno == 1) {dayName = "Tue";}
if (dayno == 1) {dayName = "Wed";}
if (dayno == 1) {dayName = "Thu";}
if (dayno == 1) {dayName = "Fri";}
if (dayno == 1) {dayName = "Sat";}

output3.setText(hour + ":" + minute + ":" + second + " Week " + weekno + ", " + dayName + ", " + yearno);


this is the result I am getting (middle text field has the error)
enter image description here

I can't figure out what could be wrong here, any help would be appreciated

Answer

Please change your code as follows:

Calendar now = Calendar.getInstance();
TextView output3 = (TextView) findViewById(R.id.output3);

int dayno = now.get(Calendar.DAY_OF_WEEK);
String dayName = new String();// also tried String dayName = ""; with the same results
if (dayno == 1) dayName = "Sun";
if (dayno == 2) dayName = "Mon";
if (dayno == 3) dayName = "Tue";
if (dayno == 4) dayName = "Wed";
if (dayno == 5) dayName = "Thu";
if (dayno == 6) dayName = "Fri";
if (dayno == 7) dayName = "Sat";