Search code examples
androidsettext

setText() displaying address instead of a value in Android


I have just started to program in Android and I am still learning. I wanted to check if a year changes automatically when a nextMonth() method is used in case of December and January or whether I should change it with a few if statements. However, I canot display the value of that, instead I get an address. Here is my code:

TextView checkMonValue;

MonthDisplayHelper currentMonth = new MonthDisplayHelper(2012, 11);
MonthDisplayHelper nextMon = currentMonth;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    checkMonValue = (TextView)findViewById(R.id.monthValue);

    checkMonValue.setText(String.valueOf(changeOfYear()));

}

public String changeOfYear(){

    nextMon.nextMonth();
    return nextMon + "" + nextMon.getYear();
}

And that is what get's displayed: Android.util.MonthDisplayHelper@44ee34e02013


Solution

  • Your are appending nextMon itself in your return value of changeOfYear() method. This way its returning the qualified name and address of nextMon as Android.util.MonthDisplayHelper@44ee34e0 appended with year as 2013.

    Please correct to append nextMon.getMonth() and nextMon.getYear()

     public String changeOfYear(){
       nextMon.nextMonth();
       return nextMon.getMonth() + "" + nextMon.getYear();
     }