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

  • SEARCH
  • Home
  • 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 1014529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:15:57+00:00 2026-05-16T10:15:57+00:00

I have a ListView with different layouts for different items. Some items are separators.

  • 0

I have a ListView with different layouts for different items. Some items are separators. Some items are different because they hold different kinds of data, etc.

I want to implement ViewHolders to speed up the getView process, but I’m not quite sure how to go about it. Different layouts have different pieces of data (which makes naming difficult) and different numbers of Views I want to use.

How should I go about doing this?

The best idea I can come up with is to create a generic ViewHolder with X items where X is the number of Views in an item layout with the highest number of them. For the other views with a small number of Views, I’ll just use a subsection of those variables in the ViewHolder. So say I have 2 layouts I use for 2 different items. One has 3 TextViews and the other has 1. I would create a ViewHolder with 3 TextView variables and only use 1 of them for my other item. My problem is that this can get really ugly looking and feels really hacky; especially when an item layout may have many Views of many different types.

Here is a very basic getView:

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

    MyHolder holder;

    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.layout_mylistlist_item, parent, false);

        holder = new MyHolder();
        holder.text = (TextView) v.findViewById(R.id.mylist_itemname);
        v.setTag(holder);
    }
    else {
        holder = (MyHolder)v.getTag();
    }

    MyListItem myItem = m_items.get(position);

    // set up the list item
    if (myItem != null) {
        // set item text
        if (holder.text != null) {
            holder.text.setText(myItem.getItemName());
        }
    }

    // return the created view
    return v;
}

Suppose I had different types of row layouts, I could have a ViewHolder for each type of row. But what type would I declare “holder” to be at the top? Or would I declare a holder for each type and then use the one for the type of row I’m on.

  • 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-16T10:15:58+00:00Added an answer on May 16, 2026 at 10:15 am

    ListView has a built in type management system. In your adapter, you have several types of items, each with their own view and layout. By overriding getItemViewType to return the data type of a given position, ListView is garunteed to pass in the correct convertview for that type of data. Then, in your getView method simply check the datatype and use a switch statement to handle each type differently.

    Each Layout type should have its own viewholder for naming clarity and ease of maintainence. Name the ViewHolders something related to each data type to keep everything straight.

    Trying to overlap everything into one ViewHolder is just not worth the effort.

    Edit
    Example

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
        int viewType = this.getItemViewType(position);
    
        switch(viewType)
        {
           case TYPE1:
    
            Type1Holder holder1; 
    
             View v = convertView; 
             if (v == null) { 
                 LayoutInflater vi = (LayoutInflater)getContext().getSystemService     (Context.LAYOUT_INFLATER_SERVICE); 
                 v = vi.inflate(R.layout.layout_mylistlist_item_type_1, parent, false); 
    
                 holder1 = new Type1Holder (); 
                 holder1.text = (TextView) v.findViewById(R.id.mylist_itemname); 
                 v.setTag(holder1); 
             } 
             else { 
                 holder1 = (Type1Holder)v.getTag(); 
             } 
    
             MyListItem myItem = m_items.get(position); 
    
             // set up the list item 
             if (myItem != null) { 
                 // set item text 
                 if (holder1.text != null) { 
                     holder1.text.setText(myItem.getItemName()); 
                 } 
             } 
    
             // return the created view 
             return v; 
    
    
         case TYPE2:
                Type2Holder holder2; 
    
             View v = convertView; 
             if (v == null) { 
                 LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                 v = vi.inflate(R.layout.layout_mylistlist_item_type_2, parent, false); 
    
                 holder2 = new Type2Holder (); 
                 holder2.text = (TextView) v.findViewById(R.id.mylist_itemname); 
                 holder2.icon = (ImageView) v.findViewById(R.id.mylist_itemicon); 
                 v.setTag(holder1); 
             } 
             else { 
                 holder2 = (Type2Holder)v.getTag(); 
             } 
    
             MyListItem myItem = m_items.get(position); 
    
             // set up the list item 
             if (myItem != null) { 
                 // set item text 
                 if (holder2.text != null) { 
                     holder2.text.setText(myItem.getItemName()); 
                 } 
    
                 if(holder2.icon != null)
                     holder2.icon.setDrawable(R.drawable.icon1);
             } 
    
    
             // return the created view 
             return v; 
    
    
           default:
               //Throw exception, unknown data type
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview with different layouts for all the listitems. My first listitem
i want to create a Listview with two different Layouts. The first item should
The objective is to have different layout for items in a ListView depending on
enter code here I have a ListView that have a different Layout XML for
I have Process objects that are monitored from two different views. A Windows.Forms.ListView (actually
I have a page with two Listviews (with two different data sources). I have
I have several similar user controls which display listviews of respectively different data entities.
I have listview control.There is an option to remove selected items.After the user removes
I have listview having customized some textview and one imageview. When I long click
I have ListView that uses a GridView to display several columns of data. Two

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.