[Solved] how to use setText

Put your problem here if it does not fit any of the other categories.

[Solved] how to use setText

Postby BackwardsDown » Thu Dec 20, 2007 9:34 pm

[Solved]

So, I am new to Android AND Java, so here is my entry to "the most easy question of the year" though it has been bugging me for a while now.

So I have created this TextView in my main.xml: (stripped from not-important info)
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <TextView id="@+id/answer"
  2.                         android:layout_width="fill_parent"
  3.                         android:layout_height="fill_parent"
  4.                         android:text="0"/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Now I want to do this: (stripped source)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Program extends Activity {
  2.     /** Called when the activity is first created. */
  3.     @Override
  4.     public void onCreate(Bundle icicle) {
  5.         super.onCreate(icicle);
  6.        
  7.         setContentView(R.layout.main);
  8.  
  9.         R.id.answer.setText("something");
  10.     }
  11. }
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


However, eclipse gives me this:
Cannot invoke setText(String) on the primitive type int


And I don't seem to get the meaning of this error. Does anyone has an idea?
Last edited by BackwardsDown on Fri Dec 21, 2007 7:11 pm, edited 1 time in total.
BackwardsDown
Freshman
Freshman
 
Posts: 5
Joined: Wed Dec 19, 2007 9:09 am

Top

Postby plusminus » Thu Dec 20, 2007 9:47 pm

Hello BackwardsDown,

everyone starts somewhen, don't mind :)

Your thoughts were basically right. But remember that the R.java contains just the IDs of the XML-Layout stuff :!:
But you want to get the View associated with that ID.

This is the evolution of the final command:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Just the ID of the View defined in XML is not pretty useful
  2. int theIDOfWhatIWantToLoad = R.id.answer;
  3.  
  4. /* But if we pass it to findViewbyID it returns us the View
  5.  * it is associated with.
  6.  * (findViewbyID returns a Object of the Type 'View') */
  7. View v = this.findViewbyID(R.id.answer);
  8.  
  9. /* We know that it is a specialized View(a TextView),
  10.  * because we defined it in the main.xml .
  11.  * So we can cast it to TextView. */
  12. TextView tv = (TextView)this.findViewbyID(R.id.answer);
  13. // And set the Text it should show:
  14. tv.setText("something");
  15.  
  16. // All in one line:
  17. ((TextView)this.findViewbyID(R.id.answer)).setText("something");
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Feel free to ask further, if you still didn't understand (or sth. else) :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby BackwardsDown » Fri Dec 21, 2007 12:01 am

Wauw, thanks for the very good explanation :D . Today I started working on tutorial 2 and tried to create my own app for the first time. And now its finished :P .

Image

*proud*
BackwardsDown
Freshman
Freshman
 
Posts: 5
Joined: Wed Dec 19, 2007 9:09 am

Postby plusminus » Fri Dec 21, 2007 12:26 am

Hello BackwardsDown,

no problem :)

Did I ever say that Android is awesome :?: :D
Also for beginners in my view.

Not many people can say, I can <rule> my own phone ^^

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests