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 sdRate=5;
//text_Rate is a TextView
text_Rate.setText(sdRate); //gives error
may be you should try like this
int sdRate=5;
//text_Rate is a TextView
text_Rate.setText(sdRate+""); //gives error
Use the Integer
class' static toString()
method.
int sdRate=5;
text_Rate.setText(Integer.toString(sdRate));
You have two options:
1) Using String.valueOf() method:
int sdRate=5;
text_Rate.setText(String.valueOf(sdRate)); //faster!, recommended! :)
2) adding an empty string:
int sdRate=5;
text_Rate.setText("" + sdRate));
Casting is not an option, will throw a ClassCastException
int sdRate=5;
text_Rate.setText(String.valueOf((String)sdRate)); //EXCEPTION!
You can use
text_Rate.setText(""+sdRate);
Did you try:
text_Rate.setText(String.valueOf(sdRate));
Source: Stackoverflow.com