This question already has an answer here:
这个问题在这里已有答案:
- How do I convert from int to String? 19 answers
如何从int转换为String? 19个答案
I have an int variable and when I am setting this variable as an Android TextView's text it's throwing an error, maybe because it's an Int. I have checked but couldn't find a toString function for the int. So how can I do that?
我有一个int变量,当我将此变量设置为Android TextView的文本时,它会抛出一个错误,可能是因为它是一个Int。我已经检查但是找不到int的toString函数。那我该怎么办呢?
int sdRate=5;
//text_Rate is a TextView
text_Rate.setText(sdRate); //gives error
6 个解决方案
#1
91
Use String.valueOf():
int sdRate=5;
//text_Rate is a TextView
text_Rate.setText(String.valueOf(sdRate)); //no more errors
#2
13
Use the Integer
class' static toString()
method.
使用Integer类的static toString()方法。
int sdRate=5;
text_Rate.setText(Integer.toString(sdRate));
#3
4
You can use
您可以使用
text_Rate.setText(""+sdRate);
#4
2
Did you try:
你试过了吗:
text_Rate.setText(String.valueOf(sdRate));
#5
1
You have two options:
你有两个选择:
1) Using String.valueOf() method:
1)使用String.valueOf()方法:
int sdRate=5;
text_Rate.setText(String.valueOf(sdRate)); //faster!, recommended! :)
2) adding an empty string:
2)添加一个空字符串:
int sdRate=5;
text_Rate.setText("" + sdRate));
Casting is not an option, will throw a ClassCastException
Casting不是一个选项,会抛出ClassCastException
int sdRate=5;
text_Rate.setText(String.valueOf((String)sdRate)); //EXCEPTION!
#6
0
may be you should try like this
也许你应该这样试试
int sdRate=5;
//text_Rate is a TextView
text_Rate.setText(sdRate+""); //gives error
#1
91
Use String.valueOf():
int sdRate=5;
//text_Rate is a TextView
text_Rate.setText(String.valueOf(sdRate)); //no more errors
#2
13
Use the Integer
class' static toString()
method.
使用Integer类的static toString()方法。
int sdRate=5;
text_Rate.setText(Integer.toString(sdRate));
#3
4
You can use
您可以使用
text_Rate.setText(""+sdRate);
#4
2
Did you try:
你试过了吗:
text_Rate.setText(String.valueOf(sdRate));
#5
1
You have two options:
你有两个选择:
1) Using String.valueOf() method:
1)使用String.valueOf()方法:
int sdRate=5;
text_Rate.setText(String.valueOf(sdRate)); //faster!, recommended! :)
2) adding an empty string:
2)添加一个空字符串:
int sdRate=5;
text_Rate.setText("" + sdRate));
Casting is not an option, will throw a ClassCastException
Casting不是一个选项,会抛出ClassCastException
int sdRate=5;
text_Rate.setText(String.valueOf((String)sdRate)); //EXCEPTION!
#6
0
may be you should try like this
也许你应该这样试试
int sdRate=5;
//text_Rate is a TextView
text_Rate.setText(sdRate+""); //gives error