Ricky Zheng
Ricky Zheng

Reputation: 1289

when i use textview.getid(),why the result comes out is not the the one i defined for textview in the xml

Assuming i got 3 data:i=01,j=02,k=03 and i want to set these 3 data indiviually into 3 textViews that defined with ids:@+id/tv01,@+id/tv02,@+id/tv03. The following is what i have tried:

if (textview.getid().contain(i))
    textView.settext(i)

But when i use textview.getid(), why the result comes out is not the the one i defined for textview in the xml? Any ideas?

Thanks for the help!

Upvotes: 1

Views: 1360

Answers (2)

Saurabh Verma
Saurabh Verma

Reputation: 6718

When you use textview.getId(), you get the id of the textview as generated in R.java file,and not what you have defined in "@+id/xxx"


So if you want to play around with ids, you should do something like :

    if(textview.getid() == R.id.textview) { 
       // do awesome stuff
    }

Upvotes: 4

Arun Badole
Arun Badole

Reputation: 11097

if you want to do it same way you are doing. You can manually set the ids of TextView by using setId().

For eg.

TextView tv1 = (TextView) findViewById(R.id.tv01);
TextView tv2 = (TextView) findViewById(R.id.tv02);
tv1.setId(1); 
tv2.setId(2);

and for getting use getId() you will get what you have set.

Upvotes: 0

Related Questions