This week's book giveaway is in the Programmer Certification forum. We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Hope no-one minds the playful title, but I thought it's an intriguing phrase that kinda describes my question.
I found some code that I then tweaked to achieve my requirement.
I'm happy that it worked but there's one part of it that I can't get my head round and I do like to try and understand code I use.
I've commented (mostly for myself really) what the code does.
The only item not declared or instantiated in the method below is an ArrayList called usableLetters.
This simply holds a collection of 12 Strings which each happen to be a single letter of the alphabet.
Here's the code:
So the bit that's puzzling me is that the variable temp is an Integer.
And yet in the first line of that for loop it appears to be accessing and getting a String (the name of a TextView).
It's not a big deal but if anyone has the time and patience to explain what I'm missing here, I'd appreciate it.
It is not retrieving a String - it is very much retrieving an int (the ID of the TextView, which is what getIdentifier returns). Likewise, findViewById takes an int parameter, not a String - what makes you think otherwise?
posted 2 weeks ago
Tim Moores wrote:It is not retrieving a String - it is very much retrieving an int (the ID of the TextView, which is what getIdentifier returns). Likewise, findViewById takes an int parameter, not a String - what makes you think otherwise?
I thought it was a String based on this example:
1. Define some TextViews
2. In OnCreate instantiate them e.g.:
where the id in R.id.usable(n) appears to be a String - ie the name of the TextView.
From what you've said, presumably java takes the usable1 in R.id.usable1 as an identifier and finds the index of that object (ie an int)?
Resource identifiers (those R.id.xyz thingies) are ints. They may look look like strings because the "usable1" designation is a string in your layout file, but the Android build system automagically translates that to be an int you can use in your source code. "R" is actually an auto-generated class, and "usable1" is an int field in that. You can see the source code to that class in build/generated/not_namespaced_r_class_sources/debug/r/your/package/name/goes/here/R.java (replace your actual package name at the hopefully obvious spot :-). The directory name may differ depending on your Gradle and Android plugin version, but it's somewhere in the "build" directory.