Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5940367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:57:29+00:00 2026-05-22T15:57:29+00:00

Can anyone please explain what’s the difference between the two kinds of getView() implementation?

  • 0

Can anyone please explain what’s the difference between the two kinds of getView() implementation?

The first one simply check if the convertView is null; if it’s null, inflate a new View object. Then set the sub-Views with proper values.

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{           
    if(convertView == null)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.itemlayout, null, true);
    }   

    ImageView image = (ImageView) convertView.findViewById(R.id.icon);
    TextView text = (TextView) convertView.findViewById(R.id.name);     

    MyItem item = items[position];
    text.setText(item.name);
    if("male".equals(item.gender))
    {
        image.setImageResource(R.drawable.male);
    }
    else if("female".equals(item.gender))
    {
        image.setImageResource(R.drawable.female);
    }

    return convertView;
}

The second one is the so-called “ViewHolder” pattern. Many developers say that this method saves a lot of memory and CPU time. But the first implementation also checks the existence of the convertView. Doesn’t the first method save some memory? Can anyone explain the difference between the two implementations more deeply and clearly? Thanks very much.

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{       
    ViewHolder holder;

    View itemView = convertView;
    if(itemView == null)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        itemView = inflater.inflate(R.layout.itemlayout, null, true);
        holder = new ViewHolder();
        holder.image = (ImageView) itemView.findViewById(R.id.icon);
        holder.text = (TextView) itemView.findViewById(R.id.name);
        itemView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) itemView.getTag();
    }

    MyItem item = items[position];
    holder.text.setText(item.name);
    if("male".equals(item.gender))
    {
        holder.image.setImageResource(R.drawable.male);
    }
    else if("female".equals(item.gender))
    {
        holder.image.setImageResource(R.drawable.female);
    }

    return itemView;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:57:30+00:00Added an answer on May 22, 2026 at 3:57 pm

    The second pattern creates a static instance of the ViewHolder and attach it to the view item the first time it is loaded, and then it will be retrieved from that view tag on the future calls

    getView() is called very frequently, expecially when you scroll big lists of elements, in fact it is called each time a listview item becomes visible on scroll.

    That prevents findViewById() to being called lots of times uselessy, keeping the views on a static reference, it is a good pattern to save some resources (expecially when you need to reference many views in your listview items).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone please explain to me why the following two queries yield different results?
Can anyone please explain what the following two paragraphs mean, in simple english? (Taken
Can anyone please explain the following HAVING clause from an Access database? How can
Can anyone please explain the usage of the character constant \000 and \xhh ie
Can anyone please explain me what is DataContractTranslstor and what's it's use?
Can anyone please explain this? struct node { int data; struct node * link;
Can anyone please explain what the following code checks for? I can make no
Can anyone please explain how this works #define maxMacro(a,b) ( (a) > (b) )
Can anyone please explain why IIS wont send message from php if FROM header
can anyone please explain how this works (asz + 7) & ~7; It rounds

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.