Hein du Plessis
Hein du Plessis

Reputation: 3357

Java: type casting question

Is there any way to do this in a single line:

    TextView tv = (TextView) findViewById(R.id.lbSightName);
    tv.setText("Some Text");

I would like to do away declaring the intermediary tv, like so:

(TextView) findViewById(R.id.lbSightName).setText("Some Text");

Not possible?

Upvotes: 0

Views: 982

Answers (6)

willcodejavaforfood
willcodejavaforfood

Reputation: 44063

Sure

((TextView) findViewById(R.id.lbSightName)).setText("Some Text");

Upvotes: 2

Deepak
Deepak

Reputation: 2102

((TextView)findViewById(R.id.lbSightName)).setText("Some thingy");

Adding one more set of parenthesis does the trick

Upvotes: 1

Stan Kurilin
Stan Kurilin

Reputation: 15792

You can, but it isn't best practice

((TextView) findViewById(R.id.lbSightName)).setText("Some Text");
TextView.class.cast(findViewById(R.id.lbSightName).setText("Some Text");

Upvotes: 5

Jan Zyka
Jan Zyka

Reputation: 17888

((TextView) findViewById(R.id.lbSightName)).setText("Some Text");

Upvotes: 4

paztulio
paztulio

Reputation: 351

((TextView) findViewById(R.id.lbSightName)).setText("Some Text");

Just add braces.

Upvotes: 2

justkt
justkt

Reputation: 14766

With one more set of parenthesis it should be possible:

((TextView)findViewById(R.id.lbSightName)).setText("Some Text");

Upvotes: 2

Related Questions