Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Walter Bueso
10,654 PointsWhy don't we just use toString()?
String name = mNameField.getText().toString();
Im new to android development and I'm having a hard time understanding the android documentation as of now, so forgive me if these are dumb questions that the documentation can answer.
1) Can someone explain to me why do we have to use getText() ? 2)Why can't we just use toString()? 3)What does getText() do that toString() can't?
IMO it would just be easier to grab the text that was inserted in mNameField and convert in into a string. Why is getText() the middle man here?
1 Answer
Jacob Bergdahl
29,119 PointsYou use getText() to get the text from the field! You see, if you use a toString()-method on just the EditText, Android Studio will think that you want to get a toString() version of the EditText itself (as in, not the text inside it, but the actual EditText). EditText (or InputField) is a View object, which has multiple properties. By using the getText() method, we are saying that we want to take the text that is contained inside the EditText!