0

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?

6 Answers 6

5

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");
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, why not best practice?
@Hein - You should prefer polymorphism / visitor pattern to instanceof/casting for dispatching in Java.
4
((TextView) findViewById(R.id.lbSightName)).setText("Some Text");

Comments

2

With one more set of parenthesis it should be possible:

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

Comments

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

Just add braces.

Comments

2

Sure

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

Comments

1

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

Adding one more set of parenthesis does the trick

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.