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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:24:34+00:00 2026-05-31T12:24:34+00:00

I’m trying to make a LazyList EndlessList but using a sectioned adapter…The sections will

  • 0

I’m trying to make a LazyList EndlessList but using a sectioned adapter…The sections will be pages of items that will be downloaded from the Internet.

I’ve tried to implement the basic gist of it using both a MergeAdapter or the deprecated SectionedAdapter (by Mark Murphy). But I’m facing issues with both of them.

1) Using the SectionedAdapter, It loads the first page fine, but on the second page I run into this exception.

Uncaught handler: thread main exiting due to uncaught exception
java.lang.ArrayIndexOutOfBoundsException
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:3572)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2487)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2353)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)

which after hours of research, i’ve realized it’s something to do with a wrong getViewTypeCount() value. But I have no idea how to implement it correctly.

2) Then I tried using the MergeAdapter, but that simply made it worse, even the Sections appear at random intervals.

I’ve written a small example program just to show my implementation.

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class LazySectionListActivity extends ListActivity implements
        OnScrollListener {

    private int mPrevItemCount;
    private int nextPrimes = 0;
    private final int MAX = 100;
    private final SectionAdapter adapter = new SectionAdapter();

    // private final MergeAdapter adapter = new MergeAdapter();

    private class SectionAdapter extends SectionedAdapter {

        @Override
        protected View getHeaderView(String caption, int index,
                                     View convertView, ViewGroup parent) {
            TextView tv = new TextView(LazySectionListActivity.this);
            tv.setText(caption);
            return tv;
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setProgressBarIndeterminate(true);
        setContentView(R.layout.main);
        getListView().setOnScrollListener(this);
        setListAdapter(adapter);
        new NextThousandPrimes(nextPrimes).execute();
        nextPrimes += MAX;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

        if(firstVisibleItem + visibleItemCount >= totalItemCount
           && mPrevItemCount != totalItemCount) {
            new NextThousandPrimes(nextPrimes).execute();
            nextPrimes += MAX;
            mPrevItemCount = totalItemCount;
        }

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    private class NextThousandPrimes extends AsyncTask<Void, Void, Void> {
        private final int start;
        private final List<String> primes = new ArrayList<String>();

        public NextThousandPrimes(int nextPrimes) {
            this.start = nextPrimes;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            setProgressBarIndeterminateVisibility(true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            for(int s = start; s < start + MAX; s++)
                if(BigInteger.valueOf(s).isProbablePrime(1)) {
                    primes.add(String.valueOf(s));
                }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            setProgressBarIndeterminateVisibility(false);
            //TextView view = new TextView(LazySectionListActivity.this);//for merge adapter, use the commented code
            //view.setText(String.format("%d-%d", start, start + MAX));
            // adapter.addView(view);
            // adapter.addAdapter(new
            // ArrayAdapter<String>(LazySectionListActivity.this,
            // android.R.layout.simple_list_item_1,
            // primes));
            adapter.addSection(String.format("%d-%d", start, start + MAX),
                               new ArrayAdapter<String>(LazySectionListActivity.this,
                                                        android.R.layout.simple_list_item_1,
                                                        primes));
            adapter.notifyDataSetChanged();

        }
    }
}

So what am i missing? Any help is appreciated. Thanks in advance.

  • 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-31T12:24:37+00:00Added an answer on May 31, 2026 at 12:24 pm

    As stated in the MergeAdapter documentation:

    You must pour the contents into the MergeAdapter before calling setListAdapter() to associate the MergeAdapter with a ListView. This limitation is required because Android only calls getViewTypeCount() once, and adding more views or adapters adds more view types.

    Hence, MergeAdapter does not work in an endless setting, because we do not know how many possible view types there are.

    You are welcome to try hacking a copy of MergeAdapter, where you wire getViewTypeCount() to be some value you are sure will be bigger than your needs, and see if that works. As the saying goes, your mileage may vary… 🙂

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.