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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:45:55+00:00 2026-06-18T17:45:55+00:00

I have been trying to look online for any solutions or examples on how

  • 0

I have been trying to look online for any solutions or examples on how to do that, but could not manage to find anything what resembles my problem.

I have a LinearLayout where I want to add/remove Views when ArrayList data changes.

As far as I understand, the only way is creating a CustomView by extending AdapterView and using ArrayAdapter.

Unfortunately I don’t understand the proper data-flow to solve this problem.

Where do I specify in CustomView which View is the container?
Can I just cast CustomView on LinearLayout when I will have implemented it?

EDIT:
I emphasize – I do not need a ListView. I need it for a CustomView

  • 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-18T17:45:56+00:00Added an answer on June 18, 2026 at 5:45 pm

    You do not need to extend AdapterView to generate your views from adapter in your custom view. You can extend LinearLayout and handle adapter. The simplest solution would look like:

    public class CustomAdapterView extends LinearLayout {
    
        private Adapter adapter;
        private final DataSetObserver observer = new DataSetObserver() {
    
            @Override
            public void onChanged() {
                refreshViewsFromAdapter();
            }
    
            @Override
            public void onInvalidated() {
                removeAllViews();
            }
        };
    
        public CustomAdapterView(Context context) {
            super(context);
        }
    
        public CustomAdapterView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomAdapterView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public Adapter getAdapter() {
            return adapter;
        }
    
        public void setAdapter(Adapter adapter) {
            if (this.adapter != null) {
                this.adapter.unregisterDataSetObserver(observer);
            }
            this.adapter = adapter;
            if (this.adapter != null) {
                this.adapter.registerDataSetObserver(observer);
            }
            initViewsFromAdapter();
        }
    
        protected void initViewsFromAdapter() {
            removeAllViews();
            if (adapter != null) {
                for (int i = 0; i < adapter.getCount(); i++) {
                    addView(adapter.getView(i, null, this), i);
                }
            }
        }
    
        protected void refreshViewsFromAdapter() {
            int childCount = getChildCount();
            int adapterSize = adapter.getCount();
            int reuseCount = Math.min(childCount, adapterSize);
    
            for (int i = 0; i < reuseCount; i++) {
                adapter.getView(i, getChildAt(i), this);
            }
    
            if (childCount < adapterSize) {
                for (int i = childCount; i < adapterSize; i++) {
                    addView(adapter.getView(i, null, this), i);
                }
            } else if (childCount > adapterSize) {
                removeViews(adapterSize, childCount);
            }
        }
    }
    

    As the code above is only a simple example it does not handle situation where adapter returns views of different types (e.g. Adapter#getViewTypeCount() returns number greater than 1).

    Of course all methods defined LinearLayout for adding/removing views are available so they may collide with your adapter handling. You could disable them by throwing UnsupportedOperationException:

        @Override
        public void addView(View child) {
            throw new UnsupportedOperationException(
                    "You cannot add views directly without adapter!");
        }
    

    (and so on for all other add/remove methods), or by overriding them to manipulate adapter’s backing dataset (which should be forced to implement your custom defined interface allowing such modifications next to Adapter interface). In both cases remember to call add remove methods from superclass in your code for adapter handling.

    EDIT:
    And simple implementation extending LinearLayout with support for Adapter’s viewTypesCount:

    class CustomAdapterViewTypedImpl extends LinearLayout {
    
        private Adapter adapter;
        private SparseArray<List<View>> typedViewsCache = new SparseArray<List<View>>();
        private final DataSetObserver observer = new DataSetObserver() {
    
            @Override
            public void onChanged() {
                refreshViewsFromAdapter();
            }
    
            @Override
            public void onInvalidated() {
                removeAllViews();
            }
        };
    
        public CustomAdapterViewTypedImpl(Context context) {
            super(context);
        }
    
        public CustomAdapterViewTypedImpl(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomAdapterViewTypedImpl(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public Adapter getAdapter() {
            return adapter;
        }
    
        public void setAdapter(Adapter adapter) {
            if (this.adapter != null) {
                this.adapter.unregisterDataSetObserver(observer);
            }
            this.adapter = adapter;
            if (this.adapter != null) {
                this.adapter.registerDataSetObserver(observer);
            }
            initViewsFromAdapter();
        }
    
        protected void initViewsFromAdapter() {
            typedViewsCache.clear();
            removeAllViews();
            View view;
            if (adapter != null) {
                for (int i = 0; i < adapter.getCount(); i++) {
                    view = adapter.getView(i, null, this);
                    addToTypesMap(adapter.getItemViewType(i), view, typedViewsCache);
                    addView(view, i);
                }
            }
        }
    
        protected void refreshViewsFromAdapter() {
            SparseArray<List<View>> typedViewsCacheCopy = typedViewsCache;
            typedViewsCache = new SparseArray<List<View>>();
            removeAllViews();
            View convertView;
            int type;
            for (int i = 0; i < adapter.getCount(); i++) {
                type = adapter.getItemViewType(i);
                convertView = shiftCachedViewOfType(type, typedViewsCacheCopy);
                convertView = adapter.getView(i, convertView, this);
                addToTypesMap(type, convertView, typedViewsCache);
                addView(convertView, i);
            }
        }
    
        private static void addToTypesMap(int type, View view, SparseArray<List<View>> typedViewsCache) {
            List<View> singleTypeViews = typedViewsCache.get(type);
            if(singleTypeViews == null) {
                singleTypeViews = new ArrayList<View>();
                typedViewsCache.put(type, singleTypeViews);
            }
            singleTypeViews.add(view);
        }
    
        private static View shiftCachedViewOfType(int type, SparseArray<List<View>> typedViewsCache) {
            List<View> singleTypeViews = typedViewsCache.get(type);
            if(singleTypeViews != null) {
                if(singleTypeViews.size() > 0) {
                    return singleTypeViews.remove(0);
                }
            }
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found similar solutions online but none that I've been able to apply
I have been trying to understand how vertex textures work, but do not understand
I have been trying to look into this and it seems hard to find
I have been trying to change the cursor style to look like a pointer
Have been trying to encrypt an xml file to a string so that I
I have been trying to serialize a list that contains arrays and lists. I
I've been trying to look for online articles / tutorials on how to go
I have been looking online for days trying to solve my problems and I
have been trying to look around the web for a good wcf publish/subscribe framework
I am fairly new to Excel VBA and have been trying to look for

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.