English subtitles

← Casting with findViewById

Get Embed Code
13 Languages

Showing Revision 6 created 05/24/2016 by Udacity Robot.

  1. I added the code that we were
    just looking at into our app.
  2. It says View orderSummaryTextView
    = findViewById,
  3. then we pass in the resource ID for
    the order_summary_text_view.
  4. But this causes an error in
    Android Studio on the next line.
  5. It says it cannot resolve
    the method setText.
  6. If I add another line of code here,
  7. I can show you the types of methods
    that are valid on this variable.
  8. So, we just created
    the orderSummaryTextView variable here,
  9. and it's a TypeView, and so
  10. on the next line,
    we should be able to call methods on it.
  11. But if you look at
    the autocomplete suggestions,
  12. these methods are actually View methods.
  13. There's no TextView specific methods.
  14. For example, there's no setText method.
  15. That's why we get an error down
    here when we try to call setText.
  16. You also can't set text size, or
    set text color, or any of those.
  17. So where are all those methods?
  18. Well, the thing is that a TextView
    is a more specific type of View.
  19. So if we want to call TextView methods
  20. on this object we need to make sure
    that this is a TextView object.
  21. Okay.
    Now the variable is a TextView object.
  22. But then I get another error that says
    incompatible types, because a TextView
  23. is required on the right hand side,
    but we're finding just a regular view.
  24. That's because the findViewById
    method returns a View.
  25. So how do we reconcile this?
  26. Well, since TextView is a more specific
    type of View or put another way,
  27. a TextView is a subclass of view, we can
    actually use something called casting.
  28. Casting in Java means that we can take
    the View returned by this method and
  29. actually treat it as a TextView.
  30. So now this right hand side is
    actually a TextView object, and
  31. then we can store it inside this
    orderSummaryTextView variable.
  32. I know that this must be really
    overwhelming because I just mentioned
  33. a ton of new concepts.
  34. Let me try to explain again.
  35. The findViewById method turns
    an object of type View.
  36. By writing this notation,
  37. parentheses with the desired object
    data type in the middle of it.
  38. We're casting this View into a TextView.
  39. So the whole expression here
    evaluates to a TextView object.
  40. Then, we can store that in the variable,
    on the left-hand side,
  41. which is orderSummary TextView,
    and it has a type TextView.
  42. Now, on subsequent lines, we can call
    TextView methods on this object.
  43. Do you see now how all the text
    methods are showing up here?
  44. setAllCaps, setTextSize, and more.
  45. So I can just erase this now,
    because setText should work,
  46. and there's no more errors.
  47. So if we want to be able
    to call TextView methods,
  48. we need to have a TextView object.
  49. In order to have a TextView object,
    We need to call findViewById,
  50. and then cast this View
    object into a TextView.
  51. The reason why casting works is because
  52. this View here in the view
    hierarchy is actually a TextView.
  53. If we we try to cast it into something
    like a radio button, that wouldn't work,
  54. because the orderSummaryTextView
    is actually a TextView, so
  55. we have to cast it into
    the appropriate type.
  56. Similarly, in the displayQuantity
    method, we're calling findViewById and
  57. we're passing in this resource ID for
    the quantity TextView.
  58. This returns a View, and
    we cast it into a TextView, and
  59. we can store that inside this
    variable called quantityTextView.
  60. This variable has type TextView, and so,
  61. on subsequent lines, we can TextView
    methods on it, like setText.
  62. Here's the updated MainActivity
    code using casting.
  63. We can cast these Views into TextViews
    so that we can store them in variables
  64. that have type TextView so
    in this list of MainActivity variables,
  65. now quantityTextView and
    orderSummaryTextView are type TextView.
  66. We can call TextView methods on these,
  67. and we're not just limited
    to treating them as Views.
  68. And now we can call TextView
    specific methods on these variables.
  69. For example we can call setText
    on the orderSummaryTextView,
  70. and we can call setText
    on the quantityTextView.
  71. As you can see,
  72. you have to be careful about what
    data types are being used where.
  73. So far, when we've called methods, we
    haven't worried about the return value,
  74. because the setText method
    actually has no return value.
  75. Same with
    the imageView.setImageResource method.
  76. This has no return value.
  77. But other methods can
    have return values.
  78. For example, in the TextView class
    we have a getTextSize method.
  79. This returns an integer, so
  80. we can store the result of this method
    in a variable called size of type int.
  81. Here's another method
    that has a return value.
  82. In the Button class you can
    call the getHeight method,
  83. and this returns an integer value, so
    you can store it inside a variable.
  84. If you tried to store this result
    inside a string, for example,
  85. you would get an error,
    because this method returns an integer.
  86. In order to know what type of data needs
    to be passed into a method and what data
  87. type gets returned back from a method,
    you should check the documentation.
  88. And for example, in the TextView class,
  89. if you call any of these methods,
    it returns a value of type int.
  90. The return value data type is
    in the left-hand column here.
  91. For other methods, you might get
    void as a return value, or booleans,
  92. which we haven't learned about yet,
    or floats or longs and etc.
  93. This documentation page also shows
    what are the expected data types for
  94. the inputs for each method.
  95. For example, the setTextColor method
    requires an integer as input and
  96. the return value is void,
    so there's no return value.
  97. So check the documentation to know
    what data types are expected.
  98. Otherwise, you'll get errors
    in Android Studio saying that
  99. the data types don't match.
  100. Now it's your turn to practice.
  101. Below are four different code examples.
  102. Some of the code may contain errors
    relating to data types not matching.
  103. If you find an error,
  104. describe how you're supposed to fix it
    by writing it in the boxes provided.
  105. Otherwise, you can just write No
    error if the code is perfectly fine.
  106. This is going to be challenging
    quiz because we just covered
  107. a lot of concepts, and
    this code is still pretty new to you.
  108. Take your time and check
    the documentation for these methods.
  109. Make sure that the inputs and
    the return values are correct and
  110. that, if we're declaring variables, that
    we have the correct data types here.
  111. Also, look up the documentation for
  112. these method calls to make sure that
    we're using the methods correctly.