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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:04:07+00:00 2026-05-16T16:04:07+00:00

I have a list that gets refreshed every 2 seconds via the Handler postDelayed()

  • 0

I have a list that gets refreshed every 2 seconds via the Handler postDelayed() method.

Every 2 seconds an AsyncTask is run that makes an HTTP GET request, turns the JSON into a list of objects, then sets the ListAdapter:

 MyListAdapter adapter = new MyListAdapter(someObjects);
 setListAdapter(adapter);

My problem is that every time the task completes (so, roughly every two seconds) my list jumps back to the top, even if I have scrolled down to the middle or bottom of the list. This would be very annoying to the end user, so I need the list to update in the background, as it’s doing, but the current view of the list to not jump back to the top at the completion of the AsyncTask.

I can include any more code needed. I’m somewhat new to android development, so I’m not sure what is helpful to others.

Additional Information

Taking suggestions from hacksteak25, I’m able to get to the point where I try to remove all the data from the adapter, then add it back one object at a time. This isn’t the end solution since this would probably still cause the screen to jump, but I’m trying to use it as a proof of concept for how I could merge the data at some point.

My problem is that I call the following code:

 MyListAdapter adapter = (MyListAdapter)getListAdapter();
 adapter.clear();
 for(MyObject myObject : myObjects)
 {
  adapter.add(myObject);
 }

After the first call to “add(myObject)” the getView() method of the MyListAdapter is being called. The private internal ArrayList of the custom adapter is empty at this point, either because I set the adapter with no myObjects in onCreate() or because I called clear() on the adapter, I’m not sure. Either way, this causes getView to fail since there are no objects in the ArrayList to be getting the view from.

getView() looks like this:

public View getView(int position, View convertView, ViewGroup parent)
{
 ViewHolder holder;
 LayoutInflater mInflater = getLayoutInflater();

 if (convertView == null)
 {
  convertView = mInflater.inflate(R.layout.myObject, null);

  holder = new ViewHolder();
  holder.someProperty = (TextView)convertView.findViewById(R.id.someProperty);
  holder.someOtherProperty = (TextView)convertView.findViewById(R.id.someOtherProperty);
  holder.someOtherOtherProperty = (TextView)convertView.findViewById(R.id.someOtherOtherProperty);

  convertView.setTag(holder);
 }
 else
 {
  holder = (ViewHolder)convertView.getTag();
 }

 // Bind the data efficiently with the holder.
 holder.someProperty.setText( mObjects.get(position).getSomeProperty());
 ...

That last line is the one that causes an IndexOutOfBoundsException.

How should I handle this situation where I get the data I want in there without causing the list to jump?

  • 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-16T16:04:07+00:00Added an answer on May 16, 2026 at 4:04 pm

    I think the preferred way is to update the adapter itself and not to replace it. Maybe you can write a method that merges the new and old data using the adapters insert() and remove() methods. I think that should keep your position.

    Added Information:

    I use the following as basic structure. Maybe it helps.

    
    public class PlaylistAdapter extends ArrayAdapter<Playlist> {
    
        private ArrayList<Playlist> items;
    
        public PlaylistAdapter(Context context, int textViewResourceId, ArrayList<Playlist> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a list form that gets attached to a main form in
I have a VBA Word Macro that gets words from .txt list and color
I have a Java program that is supplied a directory name, gets a list
I have a List in my smalltalk application that gets updated by user events.
So I have a list that gets generated e.g. missingfromauthoratative and with that list
I have a list in my python program that gets new items on certain
I have a list that is empty at first, and gets populated dynamically with
I have a list that contains 10**7 lists in the format: big_list = [[1,
I have a list that looks like this: l1 = ['200:200', '90:728'] I have
I have a list that has two items: projects and groups. Projects can go

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.