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

  • Home
  • SEARCH
  • 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 6082161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:15:30+00:00 2026-05-23T11:15:30+00:00

I am a begginer in Android but I tried to make a custom listview

  • 0

I am a begginer in Android but I tried to make a custom listview filtering and I it worked somehow. The only problem I have is that the ArrayList that I kept all the values ( “original” ArrayList ) , is getting lower and lower on items in every filtering. I can’t explain this but I thought that you can help me somehow .

Anyway here is the Custom ArrayAdaptor :

public class PkmnAdapter extends ArrayAdapter<Pkmn> {

private ArrayList<Pkmn> original;
private ArrayList<Pkmn> fitems;
private Filter filter;

public PkmnAdapter(Context context, int textViewResourceId, ArrayList<Pkmn> items) {
        super(context, textViewResourceId, items);
        this.original = items;//new ArrayList<Pkmn>();
        this.fitems = items;//new ArrayList<Pkmn>();
}

@Override
public void add(Pkmn item){
    original.add(item);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        Pkmn pkmn = original.get(position);
        if (pkmn != null) {
                TextView tt = (TextView) v.findViewById(R.id.RlabPName);
                TextView dex = (TextView)v.findViewById(R.id.RlabDex);
                ImageView img = (ImageView)v.findViewById(R.id.RimgPkmn);

                if (tt != null) { tt.setText(pkmn.getName()); }
                if (dex != null){ dex.setText(CalcDex(pkmn.getId())); }
                if (img != null){
                    int resId = getContext().getResources().getIdentifier("dex" + pkmn.getId(), "drawable", "com.compileguy.pokebwteam");
                    img.setImageResource(resId);
                }
        }
        return v;
}

@Override
public Filter getFilter()
{
    if (filter == null)
        filter = new PkmnNameFilter();

    return filter;
}

private class PkmnNameFilter extends Filter
{
        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {   
            FilterResults results = new FilterResults();
            String prefix = constraint.toString().toLowerCase();

            if (prefix == null || prefix.length() == 0)
            {
                ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
                results.values = list;
                results.count = list.size();
            }
            else
            {
                final ArrayList<Pkmn> list = original;

                int count = list.size();
                final ArrayList<Pkmn> nlist = new ArrayList<Pkmn>(count);

                for (int i=0; i<count; i++)
                {
                    final Pkmn pkmn = list.get(i);
                    final String value = pkmn.getName().toLowerCase();

                    if (value.startsWith(prefix))
                    {
                        nlist.add(pkmn);
                    }
                }
                results.values = nlist;
                results.count = nlist.size();
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            fitems = (ArrayList<Pkmn>)results.values;
            clear();
            int count = fitems.size();
            for (int i=0; i<count; i++)
            {
                Pkmn pkmn = (Pkmn)fitems.get(i);
                add(pkmn);
            }

            if (fitems.size() > 0)
                notifyDataSetChanged();
            else
                notifyDataSetInvalidated();
        }

    }


private String CalcDex(int id){
    String s = String.valueOf(id);
    if (s.length() == 1)
        s = "00"+s;
    else if (s.length() == 2)
        s = "0"+s;
    return '#'+s;
}

}

NOTE: The listview is showing correctly the items but when for exaple I remove a letter in the editbox ( which triggers the filtering ) this is where the problems start.

— EDIT —

@Janusz: Many thanks for your answer . That solved my problem .

Here is the source code that works for me , so if anyone has the same issue they could try this one :

private ArrayList<Pkmn> original;
private ArrayList<Pkmn> fitems;
private Filter filter;

public PkmnAdapter(Context context, int textViewResourceId, ArrayList<Pkmn> items) {
        super(context, textViewResourceId, items);
        this.original = new ArrayList<Pkmn>(items);
        this.fitems = new ArrayList<Pkmn>(items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        Pkmn pkmn = fitems.get(position);
        if (pkmn != null) {
                TextView tt = (TextView) v.findViewById(R.id.RlabPName);
                TextView dex = (TextView)v.findViewById(R.id.RlabDex);
                ImageView img = (ImageView)v.findViewById(R.id.RimgPkmn);

                if (tt != null) { tt.setText(pkmn.getName()); }
                if (dex != null){ dex.setText(CalcDex(pkmn.getId())); }
                if (img != null){
                    int resId = getContext().getResources().getIdentifier("dex" + pkmn.getId(), "drawable", "com.compileguy.pokebwteam");
                    img.setImageResource(resId);
                }
        }
        return v;
}

@Override
public Filter getFilter()
{
    if (filter == null)
        filter = new PkmnNameFilter();

    return filter;
}

private class PkmnNameFilter extends Filter
{
        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {   
            FilterResults results = new FilterResults();
            String prefix = constraint.toString().toLowerCase();

            if (prefix == null || prefix.length() == 0)
            {
                ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
                results.values = list;
                results.count = list.size();
            }
            else
            {
                final ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
                final ArrayList<Pkmn> nlist = new ArrayList<Pkmn>();
                int count = list.size();

                for (int i=0; i<count; i++)
                {
                    final Pkmn pkmn = list.get(i);
                    final String value = pkmn.getName().toLowerCase();

                    if (value.startsWith(prefix))
                    {
                        nlist.add(pkmn);
                    }
                }
                results.values = nlist;
                results.count = nlist.size();
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            fitems = (ArrayList<Pkmn>)results.values;

            clear();
            int count = fitems.size();
            for (int i=0; i<count; i++)
            {
                Pkmn pkmn = (Pkmn)fitems.get(i);
                add(pkmn);
            }
        }

    }
}
  • 1 1 Answer
  • 1 View
  • 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-23T11:15:31+00:00Added an answer on May 23, 2026 at 11:15 am

    Your problem are this lines:

    this.original = items;
    this.fitems = items;
    

    Items is the list you use for your ListView and putting it in two different variables does not make two different lists out of it. You are only giving the list items two different names.

    You can use:

    this.fitems = new ArrayList(items);
    

    that should generate a new List and changes on this list will only change the fitems list.

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

Sidebar

Related Questions

i am begginer to jquery and have a simple problem.. I want to make
I am a begginer in android,here I have activity that use web service: SoapObject
I beginer programmer,and don't have any QA experience (only simple test that i write
I am absolute begginer in android devlopment and i want to make a camera
I started using zombiejs, but i have some begginer questions: 1.) How testing ajax
I have spent a lot of time to resolve this problem. I'm begginer in
I am a begginer in android. I have a simple code, where I am
I`m quite begginer at WPF. I have checkBox and I want that every check
Begginer to Android development. I am developing a application, which have three different tabs.
I'am begginer in android programming and I'am trying to create app that is logging

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.