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) Custom ListViews getView() and the ViewHolder Pattern

Ganta Hemanth
Ganta Hemanth
6,460 Points

getview() method of the adapter

The following is my adapter get view code..

public View getView(int position, View convertView, ViewGroup parent) {

        viewHolder holder;
        if(convertView==null){

            convertView = LayoutInflater.from(mContext).inflate(R.layout.daily_list_item,null);
            holder = new viewHolder();
            holder.iconImageView = (ImageView)convertView.findViewById(R.id.iconimageview);
            holder.dayLabel = (TextView) convertView.findViewById(R.id.daynamelabel);
            holder.temperatureLabel=(TextView) convertView.findViewById(R.id.temperatureLabel);
            convertView.setTag("holder");
        }
        else
        {
            holder = (viewHolder) convertView.getTag();
        }
        return null;
    }

How does convert view work? what is the significance of setTag() in the code??

I tried reading online sources but i couldn't get it solved. S

1 Answer

Hey Ganta Hemanth

I could write out the answer here... but everything is explained wonderfully in this video.

https://www.youtube.com/watch?v=wDBM6wVEO70

It's straight from the guys at Google. The answer to your question starts at around 5:50, but I HIGHLY recommend watching the entire video.

This is a great video, you should check it out to answer your question :]

Ganta Hemanth
Ganta Hemanth
6,460 Points

THanks for the reply.. It really helped.. Can you help me with this question??

I really don't understand how recycle view works. I am comfortable with using the view holder in the list view but i just don't get what is going on with the recycle view.

So how does recycle view work?? What makes it better than the list view with view holder?