Reputation: 851
I'm a beginner android/java programmer and my background is primarily in C++ and C#. In C# if I have a string variable called myWord and it has a value of "Hello" I can append additional information by using the + operator.
I tried this method a few times in java and apparently I can't use this tatic because the TextView datatype is void. Android studio gives me the following error: Operator '+' cannot be applied to 'void', 'java.lang.String'
/*C # */
public string bob ()
{
return "Bob!";
}
string myWord = "Hello ";
myWord = myWord + "Bob!"; //myWord is now equal to "Hello Bob!"
OR
myWord = "Hello " + bob(); //myWord is now equal to "Hello Bob!"
*/ JAVA */
TextView displayTextView = null;
displayTextView.setText("Hello");
I'd like to find a way to append additional text to the original value of "Hello" so the value of displayTextView will be "Hello Bob!"
Any ideas?
Edit: I'm currently using a capital "S" and my app can successfully access and retrieve information from my class. The app would FC whenver I tried to append text to to the TextView data type.
Thank you for the feedback everyone.
Upvotes: 24
Views: 55938
Reputation: 3973
You can call append()
on a TextView object.
In your case it would be: displayTextView.append("Bob!");
Upvotes: 46
Reputation: 2561
Your issue is on your declaration of the String instance in both the method and the variable.
It requires a "S" not a lower case s.
Also the "+" sign does work, it is just your String declaration as pointed out.
All the best :)
Upvotes: 1
Reputation: 2679
First letter of String is not small letter. To take a String variable in java you have to write String var;
So, for android use following code:
TextView displayTextView = null;
TextView displayTextView = (TextView) findViewById(R.id.myText);
String myWord = "Your";
displayTextView.setText("Hello " + myword);
This should work.
Upvotes: 1
Reputation: 12304
Why nobody suggested getText()
method ?
TextView displayTextView = null;
displayTextView.setText("text1");
displayTextView.setText(displayTextView.getText() + "text2");//poor and weak
or better for longer strings:
SpannableString ss =new SpannableString();
ss.append("text1").append("text2");
displayTextView.setText(ss);
Upvotes: 3