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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:26:59+00:00 2026-05-25T00:26:59+00:00

I have a ListView which potentially contains thousands of rows, generated by a CursorAdapter.

  • 0

I have a ListView which potentially contains thousands of rows, generated by a CursorAdapter. Only eight or so rows are visible at any one time. I’ve had reports that starting up this view can take many seconds, and can cause an ANR (force close).

I’m doing the DB query in a background thread. I’ve verified that newView and bindView in my adapter are only being called for the number of visible rows.

Once the list is displayed, actually scrolling up and down the list is very fast.

The delay is in the call to ListView.setListAdapter, which has to run on the UI thread. Why does this seem to depend on the total number of rows in the result set, rather than the (much smaller) number of rows which are actually being displayed? Is there any way I can optimize it?

This question was asked a couple of years ago in this thread. I’m hoping to get some fresh insight and more concrete examples of potential workarounds.

UPDATE

I have tried to work around this by using CommonsWare’s EndlessAdapter. I limit the initial query to (say) 20 rows using a LIMIT clause in my DB query, and I increase this limit and resubmit the query every time I hit the bottom of the list.

(As an aside, I haven’t found a way of appending just the new results to an existing Cursor, so I’m increasing the LIMIT value each time and then re-fetching the whole lot up to the new limit in a new Cursor.)

Strangely, this workaround doesn’t seem to improve the time it takes to perform the initial setListAdapter call. When I run it on a data set containing only 20 rows, the call to setListAdapter is really quick. When I run it on a data set containing hundreds of rows, but limited to return just 20, it takes over a second.

UPDATE 2

By forcing the query to execute in the background thread with a simple getCount(), as suggested by CommonsWare, I’ve cured the initial blocking of the UI thread on starting the activity. The UI is still blocked, though, when returning to this activity from a child activity. The ListActivity by default seems to want to re-run the query on the UI thread.

I have worked around this by removing the adapter in onStop(), and recreating it in onStart(). Thus the query is always performed in the background regardless of the direction we’re moving through the activity stack.

  • 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-25T00:27:00+00:00Added an answer on May 25, 2026 at 12:27 am

    I’m doing the DB query in a background thread

    If all you do in the background is call query() or rawQuery(), the query is not actually executed. It will be lazy-executed when you first try using the Cursor (e.g., getCount()). So, the right recipe for doing a query in the background is a rawQuery() followed by something like getCount() in the background thread, to ensure the query really is executed.

    When I run it on a data set containing hundreds of rows, but limited to return just 20, it takes over a second.

    Off the cuff, that would suggest that the speed issue is not the time required to read in the results, but rather in computing the results in the first place.

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

Sidebar

Related Questions

I have a ListView which its contents could change at any time (eg. a
I have a custom listview which changes size when one of the rows is
Hallo all, I have a ListView which contains a Button in each line. The
i have a ListView which contains objects bound from an collection. The representation of
I have one listview.which contain one image ,text and one button .i had creted
This is probably easy as well. But I have this listview which contains exe
I have a listview NoteList which contains a method doListRefresh() to select list contents
I currently have a listview which contains a couple of strings. These are called
I have ListView which as one TextView in each Row. and im discovering some
I have a ListView which contains EMPLOYEE_NAME from a DB table EMPLOYEE. Some employees

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.