Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Lists with RecyclerViews Custom Adapters and ViewHolder in RecyclerViews

using findViewById(); in another class

in this example what difference in using findViewById(R.id.timeLabel); without calling it through itemView for example? , and whay he call findViewById(R.id.timeLabel); using itemView?? what does mean ??

  public class HourViewHolder extends RecyclerView.ViewHolder {

        public TextView mTimeLabel;
        public TextView mSummaryLabel;
        public TextView mTemperatureLabel;
        public ImageView mIconImageView;

        public HourViewHolder(View itemView) {
            super(itemView);

            mTimeLabel = (TextView) itemView.findViewById(R.id.timeLabel);
            mSummaryLabel = (TextView) itemView.findViewById(R.id.summaryLabel);
            mTemperatureLabel = (TextView) itemView.findViewById(R.id.temperatureLabel);
            mIconImageView = (ImageView) itemView.findViewById(R.id.iconImageView);
        }
    }
    ```

2 Answers

Hi Yazan,

Short answer: Your comparing a class that extends Activity with another class that extends RecyclerView.ViewHolder. In an activity you have the findViewById(int) function, but the RecyclerView.ViewHolder doesn't. its a totally different thing don't get confused by them. Best way to learn the difference is to read the docs here and here

now concerning how is itemView.findViewById(R.id.timeLabel); is possible, itemView is an instance of View which also does have the findViewById method.

Hope this kinda cleared it up for you. Good luck! :-)

Hi Adam <br/> yes you are right, i did not note this point thanks for explain, but there is to findViewById() method in class View and Activity there is any difference between them??

both do the same thing, from the documentation you can see that findViewById(int) does the following : "Look for a child view with the given id. If this view has the given id, return this view." I encourage you to look through the docs. http://developer.android.com/reference/android/view/View.html#findViewById(int)