Set Text to Integer Value

后端 未结 2 1372
面向向阳花
面向向阳花 2020-11-30 13:43

I want to set a TextView as the value of an integer, hopefully like so:

tv.setText(int)

I tried this an I get this error.

相关标签:
2条回答
  • 2020-11-30 14:16

    Doing setText(int) you are refering to an application resource from the XML files, not the value itself.

    To set the integer propertly do the following:

    tv.setText(""+integer);
    

    or better solution:

    tv.setText(String.valueOf(integer));
    
    0 讨论(0)
  • 2020-11-30 14:28

    Generally,

    tv.setText(String.valueOf(int));
    

    If the value is inside another class? You can make a getter for the value you want in that class:

    public int getValue() {
        return value;
    }
    

    So that you can access it from the other one:

    BUT if you set a TextView to an int, it will be interpreted as an Android resource id. If you want the value of the int as your text (and not the resource it points to), make it a String first.

    tv.setText(String.valueOf(theOtherClassInstance.getValue()));
    

    EDIT If your int is firstResult as per your comment below, then the getter becomes:

    public int getFirstResult() {
        return firstResult;
    }
    
    0 讨论(0)
提交回复
热议问题