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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:52:21+00:00 2026-05-14T14:52:21+00:00

I’m trying to filter my ListView which is populated with this ArrayAdapter: package me.alxandr.android.mymir.adapters;

  • 0

I’m trying to filter my ListView which is populated with this ArrayAdapter:

package me.alxandr.android.mymir.adapters;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import me.alxandr.android.mymir.R;
import me.alxandr.android.mymir.model.Manga;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class MangaListAdapter extends ArrayAdapter<Manga> implements SectionIndexer
{
    public ArrayList<Manga> items;
    public ArrayList<Manga> filtered;
    private Context context;
    private HashMap<String, Integer> alphaIndexer;
    private String[] sections = new String[0];
    private Filter filter;
    private boolean enableSections;

    public MangaListAdapter(Context context, int textViewResourceId, ArrayList<Manga> items, boolean enableSections)
    {
        super(context, textViewResourceId, items);
        this.filtered = items;
        this.items = filtered;
        this.context = context;
        this.filter = new MangaNameFilter();
        this.enableSections = enableSections;

        if(enableSections)
        {
            alphaIndexer = new HashMap<String, Integer>();
            for(int i = items.size() - 1; i >= 0; i--)
            {
                Manga element = items.get(i);
                String firstChar = element.getName().substring(0, 1).toUpperCase();
                if(firstChar.charAt(0) > 'Z' || firstChar.charAt(0) < 'A')
                    firstChar = "@";

                alphaIndexer.put(firstChar, i);
            }

            Set<String> keys = alphaIndexer.keySet();
            Iterator<String> it = keys.iterator();
            ArrayList<String> keyList = new ArrayList<String>();
            while(it.hasNext())
                keyList.add(it.next());

            Collections.sort(keyList);
            sections = new String[keyList.size()];
            keyList.toArray(sections);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = convertView;
        if(v == null)
        {
            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.mangarow, null);
        }

        Manga o = items.get(position);
        if(o != null)
        {
            TextView tt = (TextView) v.findViewById(R.id.MangaRow_MangaName);
            TextView bt = (TextView) v.findViewById(R.id.MangaRow_MangaExtra);
            if(tt != null)
                tt.setText(o.getName());
            if(bt != null)
                bt.setText(o.getLastUpdated() + " - " + o.getLatestChapter());

            if(enableSections && getSectionForPosition(position) != getSectionForPosition(position + 1))
            {
                TextView h = (TextView) v.findViewById(R.id.MangaRow_Header);
                h.setText(sections[getSectionForPosition(position)]);
                h.setVisibility(View.VISIBLE);
            }
            else
            {
                TextView h = (TextView) v.findViewById(R.id.MangaRow_Header);
                h.setVisibility(View.GONE);
            }
        }

        return v;
    }

    @Override
    public void notifyDataSetInvalidated()
    {
        if(enableSections)
        {
            for (int i = items.size() - 1; i >= 0; i--)
            {
                Manga element = items.get(i);
                String firstChar = element.getName().substring(0, 1).toUpperCase();
                if(firstChar.charAt(0) > 'Z' || firstChar.charAt(0) < 'A')
                    firstChar = "@";
                alphaIndexer.put(firstChar, i);
            }

            Set<String> keys = alphaIndexer.keySet();
            Iterator<String> it = keys.iterator();
            ArrayList<String> keyList = new ArrayList<String>();
            while (it.hasNext())
            {
                keyList.add(it.next());
            }

            Collections.sort(keyList);
            sections = new String[keyList.size()];
            keyList.toArray(sections);

            super.notifyDataSetInvalidated();
        }
    }

    public int getPositionForSection(int section)
    {
        if(!enableSections) return 0;
        String letter = sections[section];

        return alphaIndexer.get(letter);
    }

    public int getSectionForPosition(int position)
    {
        if(!enableSections) return 0;
        int prevIndex = 0;
        for(int i = 0; i < sections.length; i++)
        {
            if(getPositionForSection(i) > position && prevIndex <= position)
            {
                prevIndex = i;
                break;
            }
            prevIndex = i;
        }
        return prevIndex;
    }

    public Object[] getSections()
    {
        return sections;
    }

    @Override
    public Filter getFilter()
    {
        if(filter == null)
            filter = new MangaNameFilter();
        return filter;
    }

    private class MangaNameFilter extends Filter
    {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // NOTE: this function is *always* called from a background thread, and
            // not the UI thread.
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if(constraint != null && constraint.toString().length() > 0)
            {
                ArrayList<Manga> filt = new ArrayList<Manga>();
                ArrayList<Manga> lItems = new ArrayList<Manga>();
                synchronized (items)
                {
                    Collections.copy(lItems, items);
                }
                for(int i = 0, l = lItems.size(); i < l; i++)
                {
                    Manga m = lItems.get(i);
                    if(m.getName().toLowerCase().contains(constraint))
                        filt.add(m);
                }
                result.count = filt.size();
                result.values = filt;
            }
            else
            {
                synchronized(items)
                {
                    result.values = items;
                    result.count = items.size();
                }
            }
            return result;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // NOTE: this function is *always* called from the UI thread.
            filtered = (ArrayList<Manga>)results.values;
            notifyDataSetChanged();
        }

    }
}

However, when I call filter(‘test’) on the filter nothing happens at all (or the background-thread is run, but the list isn’t filtered as far as the user conserns). How can I fix this?

  • 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-14T14:52:22+00:00Added an answer on May 14, 2026 at 2:52 pm

    This fixed my problem. I’m not certain it’s the best solution, but it works. My project’s open-source, so feel free to use any of the code here should it prove usefull :-).

    package me.alxandr.android.mymir.adapters;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Set;
    
    import me.alxandr.android.mymir.R;
    import me.alxandr.android.mymir.model.Manga;
    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Filter;
    import android.widget.SectionIndexer;
    import android.widget.TextView;
    
    public class MangaListAdapter extends ArrayAdapter<Manga> implements SectionIndexer
    {
        public ArrayList<Manga> items;
        public ArrayList<Manga> filtered;
        private Context context;
        private HashMap<String, Integer> alphaIndexer;
        private String[] sections = new String[0];
        private Filter filter;
        private boolean enableSections;
    
        public Manga getByPosition(int position)
        {
            return items.get(position);
        }
    
        public MangaListAdapter(Context context, int textViewResourceId, ArrayList<Manga> items, boolean enableSections)
        {
            super(context, textViewResourceId, items);
            this.filtered = items;
            this.items = ArrayList<Manga> items.clone();
            this.context = context;
            this.filter = new MangaNameFilter();
            this.enableSections = enableSections;
    
            if(enableSections)
            {
                alphaIndexer = new HashMap<String, Integer>();
                for(int i = items.size() - 1; i >= 0; i--)
                {
                    Manga element = items.get(i);
                    String firstChar = element.getName().substring(0, 1).toUpperCase();
                    if(firstChar.charAt(0) > 'Z' || firstChar.charAt(0) < 'A')
                        firstChar = "@";
    
                    alphaIndexer.put(firstChar, i);
                }
    
                Set<String> keys = alphaIndexer.keySet();
                Iterator<String> it = keys.iterator();
                ArrayList<String> keyList = new ArrayList<String>();
                while(it.hasNext())
                    keyList.add(it.next());
    
                Collections.sort(keyList);
                sections = new String[keyList.size()];
                keyList.toArray(sections);
            }
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View v = convertView;
            if(v == null)
            {
                LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.mangarow, null);
            }
    
            Manga o = filtered.get(position);
            if(o != null)
            {
                TextView tt = (TextView) v.findViewById(R.id.MangaRow_MangaName);
                TextView bt = (TextView) v.findViewById(R.id.MangaRow_MangaExtra);
                if(tt != null)
                    tt.setText(o.getName());
                if(bt != null)
                    bt.setText(o.getLastUpdated() + " - " + o.getLatestChapter());
    
                if(enableSections && getSectionForPosition(position) != getSectionForPosition(position + 1))
                {
                    TextView h = (TextView) v.findViewById(R.id.MangaRow_Header);
                    h.setText(sections[getSectionForPosition(position)]);
                    h.setVisibility(View.VISIBLE);
                }
                else
                {
                    TextView h = (TextView) v.findViewById(R.id.MangaRow_Header);
                    h.setVisibility(View.GONE);
                }
            }
    
            return v;
        }
    
        @Override
        public void notifyDataSetInvalidated()
        {
            if(enableSections)
            {
                for (int i = items.size() - 1; i >= 0; i--)
                {
                    Manga element = items.get(i);
                    String firstChar = element.getName().substring(0, 1).toUpperCase();
                    if(firstChar.charAt(0) > 'Z' || firstChar.charAt(0) < 'A')
                        firstChar = "@";
                    alphaIndexer.put(firstChar, i);
                }
    
                Set<String> keys = alphaIndexer.keySet();
                Iterator<String> it = keys.iterator();
                ArrayList<String> keyList = new ArrayList<String>();
                while (it.hasNext())
                {
                    keyList.add(it.next());
                }
    
                Collections.sort(keyList);
                sections = new String[keyList.size()];
                keyList.toArray(sections);
    
                super.notifyDataSetInvalidated();
            }
        }
    
        public int getPositionForSection(int section)
        {
            if(!enableSections) return 0;
            String letter = sections[section];
    
            return alphaIndexer.get(letter);
        }
    
        public int getSectionForPosition(int position)
        {
            if(!enableSections) return 0;
            int prevIndex = 0;
            for(int i = 0; i < sections.length; i++)
            {
                if(getPositionForSection(i) > position && prevIndex <= position)
                {
                    prevIndex = i;
                    break;
                }
                prevIndex = i;
            }
            return prevIndex;
        }
    
        public Object[] getSections()
        {
            return sections;
        }
    
        @Override
        public Filter getFilter()
        {
            if(filter == null)
                filter = new MangaNameFilter();
            return filter;
        }
    
        private class MangaNameFilter extends Filter
        {
    
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // NOTE: this function is *always* called from a background thread, and
                // not the UI thread.
                constraint = constraint.toString().toLowerCase();
                FilterResults result = new FilterResults();
                if(constraint != null && constraint.toString().length() > 0)
                {
                    ArrayList<Manga> filt = new ArrayList<Manga>();
                    ArrayList<Manga> lItems = new ArrayList<Manga>();
                    synchronized (this)
                    {
                        lItems.addAll(items);
                    }
                    for(int i = 0, l = lItems.size(); i < l; i++)
                    {
                        Manga m = lItems.get(i);
                        if(m.getName().toLowerCase().contains(constraint))
                            filt.add(m);
                    }
                    result.count = filt.size();
                    result.values = filt;
                }
                else
                {
                    synchronized(this)
                    {
                        result.values = items;
                        result.count = items.size();
                    }
                }
                return result;
            }
    
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                // NOTE: this function is *always* called from the UI thread.
                filtered = (ArrayList<Manga>)results.values;
                notifyDataSetChanged();
                clear();
                for(int i = 0, l = filtered.size(); i < l; i++)
                    add(filtered.get(i));
                notifyDataSetInvalidated();
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.