tags:

views:

99

answers:

2

While working my way through the Android tutorials, I came across something I don't understand. It's probably extremely simple, but I just need an idea why it's this way.

In the tutorial: http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

The tutorial seems to construct a new AutoCompleteTextView using:

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);

I assume their using the constructor:

AutoCompleteTextView(Context context, AttributeSet attrs)

I think their AttributeSet is the "findViewById(R.id.autocomplete_country)"; while their context is the (AutoCompleteTextView). Is this right?

Also... where's the new keyword, the comma, and why is there a pair of parenthesis?

I always thought it'd have to be:

AutoCompleteTextView textview = new AutoCompleteTextView(context here, attrs here);

Where am I going wrong?!

+1  A: 

The quoted code is not constructing an object but "getting" it. What you are doing is calling a function which returns a reference to an already constructed object. In this case, the object was probably created when the XML Layout was inflated (in the onCreate(Bundle) method of you Activity usually). The code is fine.

findViewById(R.id.something)

returns a View object. It then has to be wrapped in order to acces it as an instance of any of View's subclasses.

Tom R
Where can I find more information on "getting" an object. I have three Java books and I don't think any of them cover this topic.Thanks for the reply!
Joel
I'm pretty sure every basic Java book covers calling a method that returns an object.
mbaird
Objects can be passed as arguments in functions or returned by functions. The code above does the latter. I can define a function *public static SomeObject getSomeObject()* in the same way as I can define *public static int getSomeInt()*. (Actually, this is not entirely true, but it holds for intended purposes) Is this your first Java project?
Tom R
For the most part, yes. I have done most of the example in the books I own and done some modifications of my own, but nothing "from scratch". I would have Googled the answer, but I didn't know what to search. You all have given me a few keywords to search! :)
Joel
In that case I wouldn't recommend Android as a good starting point for learning Java. You have to work with a large, bossy SDK and there are various caveats compared to writing a command-line or Swing app.
Tom R
Hmm... perhaps I'll stick with command-line Java until I know a bit more about the "inner workings" of the language. Sad, but if that's what I have to do...
Joel
I agree, learning Java makes learning Android a smoother process. If you learn Android before Java, you'd be bouncing back and forth figuring out whats going on. Android books are written with an assumption you have some Java experience. I commend your effort though. Best of luck. :)
Anthony Forloney
Thank you, Anthony!
Joel
No problem. Any bumps along the way, just come back and we can try to help.
Anthony Forloney
I'm not sure I share the opinion that it's better to learn Java first, then move to Android. Yes, the tutorials do sometimes assume that you know Java and are just new to the Android side of things, and so this does lead to confusion. However, it's counterbalanced by the more immediate feedback and satisfaction you can get by writing Android apps. Assuming you actually have an Android phone, then you can use your apps right away, show them to your friends, etc. There are so many tutorials out there that you can teach yourself given time and patience - it's working for me!
Steve H
+3  A: 

findViewById returns the View corresponding to the argument passed in, and you cast the View to be the object of whatever the type you are working with.

Button myButton = (Button) findViewById(R.id.button);
                     ^ casting   ^ returns the View of your button.
Anthony Forloney