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 8559287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:00:25+00:00 2026-06-11T16:00:25+00:00

I have a ListView where I want to display items from more than one

  • 0

I have a ListView where I want to display items from more than one adapters, for example two SimpleCursorAdapters that come from different queries. Each adapter has its own layout, which don’t have to be the same.

I tried with the following wrapper class:

public class MultiListAdapter implements ListAdapter {

    private List<ListAdapter> adapters;

    private static class PositionResult {
        ListAdapter adapter;
        int position;
        PositionResult(ListAdapter adapter, int position) {
            this.adapter = adapter;
            this.position = position;
        }
    }

    private PositionResult getInternalPosition(int position) {
        int i = 0;
        int result = position;
        for (ListAdapter adapter: adapters) {
            int count = adapter.getCount();
            if (result < count) {
                return new PositionResult(adapter, result);
            }
            result -= count;
            ++i;
        }
        return null;
    }

    public MultiListAdapter(List<ListAdapter> adapters) {
        this.adapters = adapters;
    }

    @Override
    public int getCount() {
        int result = 0;
        int i = 0;
        for (ListAdapter adapter: adapters) {
            int c = adapter.getCount();
            result += c;
        }
        return result;
    }

    @Override
    public Object getItem(int position) {
        PositionResult p = getInternalPosition(position);
        return p.adapter.getItem(p.position);
    }

    @Override
    public long getItemId(int position) {
        PositionResult p = getInternalPosition(position);
        return p.adapter.getItemId(p.position);
    }

    @Override
    public int getItemViewType(int position) {
        PositionResult p = getInternalPosition(position);
        return p.adapter.getItemViewType(p.position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PositionResult p = getInternalPosition(position);
        return p.adapter.getView(p.position, convertView, parent);
    }

    @Override
    public int getViewTypeCount() {
        int result = 0;
        for (ListAdapter adapter: adapters) {
            result += adapter.getViewTypeCount();
        }
        return result;
    }

    @Override
    public boolean hasStableIds() {
        for (ListAdapter adapter: adapters) {
            if (!adapter.hasStableIds()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean isEmpty() {
        for (ListAdapter adapter: adapters) {
            if (!adapter.isEmpty()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        for (ListAdapter adapter: adapters) {
            adapter.registerDataSetObserver(observer);
        }

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        for (ListAdapter adapter: adapters) {
            adapter.unregisterDataSetObserver(observer);
        }
    }

    @Override
    public boolean areAllItemsEnabled() {
        for (ListAdapter adapter: adapters) {
            if (!adapter.areAllItemsEnabled()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean isEnabled(int position) {
        PositionResult p = getInternalPosition(position);
        return p.adapter.isEnabled(p.position);
    }

}

This works if I only have a few elements in each list. But if it has more, and I need to scroll, then the list breaks completely, displays the elements mixed up, and when I click on them, they even give me different positions than their real position.

What’s wrong with the code above?

  • 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-06-11T16:00:26+00:00Added an answer on June 11, 2026 at 4:00 pm

    The problem is that the getItemViewType() method returns the same value for the different views, although the actual view types are different. This makes the system try to reuse the views that are of a different type.

    That method should instead look like this:

    private int getMaxViewType() {
        int result = 1;
        for (ListAdapter adapter: adapters) {
            result = Math.max(result, adapter.getViewTypeCount());
        }
        return result;
    }
    
    @Override
    public int getItemViewType(int position) {
        PositionResult p = getInternalPosition(position);
        return p.adapterId*getMaxViewType() + p.adapter.getItemViewType(p.position);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A have a ListView that is rendered with multiple items. Now I want to
I have created a custom ArrayAdapter to display data within ListView items from an
I want to display a ListView in table format. The ListView should have rows
I have a ListView that is populated using an XML file. However, I want
I have a ListView that gets updated frequently, at irregular intervals. I want it
I have a ListView-like control that displays a list of items of various heights.
I have two activities one displaying map view and another listview, the main objective
I have a list view that can display items based on internal state (it
I want create a web application that display a list of items. Suppose I
I display first 20 records fetched from server in a listview. I have baseadapter

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.