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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:21:40+00:00 2026-05-28T02:21:40+00:00

I’m having a strange issue with a custom implementation of Android’s ArrayAdapter. To give

  • 0

I’m having a strange issue with a custom implementation of Android’s ArrayAdapter. To give some background, I’m trying update a ListView’s contents while preserving the current scroll position.

I have a service which executes a thread to update data that’s displayed in the ListView. That data is stored in an ArrayList and that ArrayList is used to generate some custom ArrayAdapters for the ListView. The adapters are also updated when an item in the ListView is pressed (either adding or removing an item). I used to just create new adapters each time there was any type of change and then set this new adapter to the ListView. This worked, but caused the ListView to scroll to the top each time. Given the nature of my application this was undesirable. The current scrolled position in the ListView must be maintained between updates.

Instead of creating new adapters I began clearing the adapter that I needed to update using the adapter’s clear() method, then rebuild the adapter’s items by using the adapter’s add() method. Both of these methods are being called on the adapter. The adapters are all set to notifyDataOnChange in their constructors so I don’t have to manually call notiftyDatasetChanged() each time (although given my issue I’ve tried calling it manually as well to no avail).

Here’s what my custom adapter looks like:

public class RealmAdapter extends ArrayAdapter<Realm>
{
    Context c;

    public RealmAdapter(Context context, int resource, int textViewResourceId)
    {               
        super(context, resource, textViewResourceId);
        setNotifyOnChange(true);
        c = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    { 
        ...
    }
...
}

Long story short, here’s my issue. When I call clear() on the adapter, the adapter is not being cleared.

Here’s a snippet from my onPostExecute in my thread that does updating. I’m being sure to put it here so it’s updating on the UI thread. I also have this exact code copied in a private method in my UI activity. This code does not work in either place:

appState.favoriteAdapter.clear();
Log.d(LOG_TAG, "COUNT: " + appState.favoriteAdapter.getCount());
for(Realm r : appState.favorites) {
    appState.favoriteAdapter.add(r);
}
Log.d(LOG_TAG, "COUNT: " + appState.favoriteAdapter.getCount());

As an example, if the above adapter had 3 items in it, calling a getCount() right after the clear() is returning 3 instead of 0. Likewise, if the appState.favorites ArrayList only has 2 items in it, the getCount() after the loop is still returning 3, not 2. Because the adapter is not responding to any of these calls it makes it impossible to update in any fashion. I can post a Logcat later if that will be helpful, but there are no exceptions or anything useful being displayed.

After busting my head for hours, the issue I appear to be having is that the adapter is not responding to calls to any methods that alter it. I’ve tried passing an empty ArrayList into the adapter’s super() call, this does not help. Am I missing something or using the ArrayAdapter incorrectly? I’ve searched all over and I’ve already checked a lot of the common problems such as modifying the underlying array and expecting it to update, not calling (or in my casing setting to the adapter) notifyDatasetChanged(), and using an unsupported operation on the underlying collection.

The declaration of the favoriteAdapter is very simple and is contained in my Application class:

public RealmAdapter favoriteAdapter;

Here is the initialization of the favoriteAdapter from above:

if(appState.favoriteAdapter == null) {
    appState.favoriteAdapter = new RealmAdapter(c, R.layout.list_item, R.layout.realm_entry, appState.favorites);
}
else {
    appState.favoriteAdapter.clear();
    Log.d(LOG_TAG, "COUNT: " + appState.favoriteAdapter.getCount());
    for(Realm r : appState.favorites) {
        appState.favoriteAdapter.add(r);
    }
    Log.d(LOG_TAG, "COUNT: " + appState.favoriteAdapter.getCount());
}

The above code is in both my UI thread and the thread that downloads the refreshed data.

Underneath the code above a filter is put in place:

if(appState.favoriteAdapter != null && RealmSelector.realmFilter != null)                   appState.favoriteAdapter.getFilter().filter(RealmSelector.realmFilter.getText().toString());

Would the filter affect clearing the list? Logic would dictate not…

  • 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-28T02:21:41+00:00Added an answer on May 28, 2026 at 2:21 am

    I had filters being applied to the custom ArrayAdapter. Apparently this interferes with adding and removing items from the adapter itself? I added this code to my method and it is now working:

    if(appState.favoriteAdapter != null && realmFilter != null) {
         appState.favoriteAdapter.getFilter().filter(realmFilter.getText().toString());
    }
    

    I’d love if anyone could explain why this matters. I thought filters were meant to select subsets of items in the adapter. In my testing I was leaving the text box that is used for the filter empty, thus no actual filter text should have been applied. Again, if someone knows what’s going on and could explain to me why this fixes the problem I’d love to know.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
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 parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.