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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:39:06+00:00 2026-05-23T10:39:06+00:00

I have been implementing a custom list adapter and custom filter. I have gotten

  • 0

I have been implementing a custom list adapter and custom filter. I have gotten to the point where I can filter out my list by typing, however when I delete my constraints the list does not repopulate. I have used these two sources to get where I am. http://www.google.com/codesearch/p?hl=it#uX1GffpyOZk/core/java/android/widget/ArrayAdapter.java&q=android%20arrayadapter&sa=N&cd=1&ct=rc

and Custom filtering in Android using ArrayAdapter

I am lost as what to do next. This is my code:

private class stationAdapter extends ArrayAdapter<Station>
{

    //======================================
    public ArrayList<Station> stations;
    public ArrayList<Station> filtered;
    private Filter filter;
    //=====================

    public stationAdapter(Context context, int textViewResourceId, ArrayList<Station> stations)
    {
        super(context, textViewResourceId, stations);
        this.filtered = stations;
        this.stations = filtered;
        this.filter = new StationFilter();
    }

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

        if (temp != null)
        {
            TextView stationName = (TextView) v.findViewById(R.id.stationname);
            TextView serviced = (TextView) v.findViewById(R.id.inservice);

            try
            {
                if (temp.isRedLine())
                {
                    // v.setBackgroundResource(R.color.red);
                    ImageView imageView = (ImageView) v.findViewById(R.id.icon);
                    imageView.setImageResource(R.drawable.redstation);
                    v.setBackgroundResource(temp.getAreaColour());
                }
                else
                {
                    ImageView imageView = (ImageView) v.findViewById(R.id.icon);
                    imageView.setImageResource(R.drawable.greenstation);

                    v.setBackgroundResource(temp.getAreaColour());
                }
            }
            catch (Exception e)
            {
                Log.d(TAG, "Null pointer");
            }
            if (stationName != null)
            {
                stationName.setText(temp.getName());
            }
            if (serviced != null)
            {
                serviced.setText(temp.getIrishName());
            }
        }
        return v;
    }

    //=====================================

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

    private class StationFilter 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<Station> filt = new ArrayList<Station>();
                ArrayList<Station> lItems = new ArrayList<Station>();
                synchronized (this)
                {
                    lItems.addAll(stations);
                }
                for(int i = 0, l = lItems.size(); i < l; i++)
                {
                    Station m = lItems.get(i);
                    if(m.getName().toLowerCase().startsWith((String) constraint))
                    {
                        filt.add(m);
                    }
                }
                result.count = filt.size();
                result.values = filt;
            }
            else
            {
                synchronized(this)
                {
                    result.values = stations;
                    result.count = stations.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<Station>)results.values;
            notifyDataSetChanged();
            clear();
            for(int i = 0, l = filtered.size(); i < l; i++){
                add(filtered.get(i));
            }
            notifyDataSetInvalidated();
        }

    }
    //===================================================

}

Do I need to override more methods from Filterable or do I need to do something with my views?

Any help would greatly be appreciated thanks.

  • 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-23T10:39:07+00:00Added an answer on May 23, 2026 at 10:39 am
            protected void cloneItems(ArrayList<Station> items) {
            for (Iterator<Station> iterator = items.iterator(); iterator
            .hasNext();) {
                Station s = (Station) iterator.next();
                originalItems.add(s);
            }
        }
    

    This is the key to the filter working. Call clone in the constructor with the list passed in and the filter works.

    Credit goes to the first answer in this post: How to write a custom filter for ListView with ArrayAdapter

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

Sidebar

Related Questions

I have been implementing value objects as custom DBAL types in Doctrine 2 and
I've been implementing a custom template matrix class and I have a function I
Im implementing a ZoomButtonsController in my custom View and have been trying to figure
I have been tasked with implementing a PKI library in C# for a company
I have been looking at using TDD and implementing proper testing (only just started
I have been studying SOAP and WSDL in preparation for implementing a web service.
I am implementing a system where I need real-time updates. I have been looking
I've been implementing a number of custom-lists, all worked fine. Now I'm trying to
I have been implementing a class which class extends ArrayAdapter and implements Filterable. The
For a personal project I have been implementing my own libstdc++. Bit by bit,

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.