0

I always bump into these when I do method overloading. How can I write the 2nd method in one line? View.setText(text) returns void, but I want to return the TextView after it executes setText().
Note: I know I can 'write it in two statments'. Question is asking how to make it in one.

private ViewGroup getExpanableListChild() {
    return (LinearLayout) LayoutInflater.from(this).inflate(
            R.layout.list_checkbox, null);
}

private ViewGroup getExpanableListChild(String text) {
    return ViewUtil.getTextViewFromParent(getExpanableListChild()).setText(
            text);//compile error, can't return void.

}
wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • 5
    You can't easily do it - what is wrong with the 2 lines? – assylias Jan 13 '13 at 19:18
  • Nothing too wrong, but I always bump into these , when I want to return sometihng after doing a void method. – wtsang02 Jan 13 '13 at 19:19
  • Do you "own" the code for ViewUtil, ViewGroup and so on? – Peter Svensson Jan 13 '13 at 19:21
  • If you want return something after calling void.. you can try use method `clone()` check documentation, it returns object - so you will get textview as you want by `tv = yourtv.clone();` or something like that or `tv = (TextView)yourtv.clone()` and this stuff you can use in your `one line..`. – deadfish Jan 13 '13 at 19:21
  • But you can't: it's Java, and Java has syntax amd semantocs you cannot avoid. Unless it's your code, what else can you do, other than create utilities? – Dave Newton Jan 13 '13 at 19:23
  • Ok thanks guys, I was just wonder if theres an easy way to return (object) after executing a void method. – wtsang02 Jan 13 '13 at 19:24
  • you can also set `textwatcher` for textview and make it to return something after changing text, lol. I think it's `public void addTextChangedListener (TextWatcher watcher)` – deadfish Jan 13 '13 at 19:25

2 Answers2

1

You can use a Builder for these cases but I don't see the point in this particular case.

For example:

return ViewUtil.getTextViewFromParent(getExpanableListChild()) //returns some kind of builder
       .setText(text) // returns builder
       .setColor(someColor) // another example, again returns builder
       .build(); // returns ViewGroup

This is the same strategy present in the SDK where you have an AlertDialog.Builder which builds AlertDialog

aromero
  • 25,681
  • 6
  • 57
  • 79
0

As others have explained, you simply can't do this without changing the implementation of ViewGroup.setText() to return a ViewGroup with the 'text' set to whatever you passed.

To do so would be a more functional programming style. Java is not a naturally functional language by design, and void methods are basically the antithesis of the functional style - i.e they have side effects (in this case setting the 'text' on the ViewGroup object).

When calling void methods, it's necessary to make the call, then do something with the resulting, modified object (in this case return it) on subsequent lines. That's just the imperative programming style.

davnicwil
  • 28,487
  • 16
  • 107
  • 123