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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:50:22+00:00 2026-06-15T21:50:22+00:00

I have a problem with my code, I perform a search into a HashMap

  • 0

I have a problem with my code, I perform a search into a HashMap inside an Adapter. It works fine but when I delete quickly all letters in EditText so the Adapter show the list of the first entire string typed and not the list of all elements.

Example:
I type James, the view get all the James in the Map, but if I delete quickly the EditText (pressing back) so the method perform the search right back and show the correct list for any substring (jame, jam, ja, j) but at the end he shows again the list inherited “James” and not the full contact list

Thank you for any answers!

public class ContactsActivity extends ListActivity {

private HashMap<String, UserEntry> all_map_jid=new HashMap<String, UserEntry>();
private ArrayList<String> all_mkey=new ArrayList<String>();



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);
    getListView().setTextFilterEnabled(true);


    final EditText searchText = (EditText) findViewById(R.id.searchbox);


    TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            //... your logic
             adapter.getFilter().filter(s.toString());

        }
        @Override
        public void afterTextChanged(Editable arg0) {
            // ... your logic

        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // ... your logic
        }
    };

    searchText.addTextChangedListener(textWatcher);}


public class RosterAdapter extends BaseAdapter implements Filterable{

    //private ViewHolder holder;
    private LayoutInflater inflater;


    private HashMap<String, UserEntry> mappa_users=null;
    private ArrayList<String> mKeys;

    public RosterAdapter(){
        this.mappa_users =new HashMap<String, UserEntry>();
        this.inflater=          (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mKeys=new ArrayList<String>();
    }
    @Override
    public int getCount() {
        return mappa_users.size();
    }

    @Override
    public UserEntry getItem(int position) {
        return mappa_users.get(mKeys.get(position));
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }


     public View getView(int position, View convertView, ViewGroup parent) {



        ViewHolder holder;


        if (convertView == null) {

            convertView = inflater.inflate(R.layout.layout_row, null);

            // Creates a ViewHolder and store references to the two children
            // views we want to bind data to.
            holder = new ViewHolder();
            holder.username = (TextView) convertView.findViewById(R.id.user_info);
            holder.availability = (ImageView) convertView.findViewById(R.id.user_availability);
            holder.user_ic = (ImageView) convertView.findViewById(R.id.icon);


            // Keep track of the view holder as a tag of the view
            convertView.setTag(holder);

        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag(); 
        }



        String user = getItem(position).getUserName();
        //Log.e("Nome","username "+user);
        holder.username.setText(user);



        if(!(getItem(position).getUserStatus())){

            System.out.println("unavailable");
            holder.availability.setImageResource(R.drawable.ic_not_available);  

        }else{

            holder.availability.setImageResource(R.drawable.ic_available);
        }

        //do your view stuff here

        return convertView;
    }





    @Override
    public Filter getFilter() {
        return new Filter() {
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {                 
                mappa_users = (HashMap<String,UserEntry>) results.values;
                mKeys= new ArrayList<String>(Arrays.asList(mappa_users.keySet().toArray(new String[mappa_users.size()])));
                Collections.sort(mKeys, new RosterEntryComparator(mappa_users));

                notifyDataSetChanged();
                }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                HashMap<String,UserEntry> searched_user=new HashMap<String,UserEntry>();
                FilterResults results = new FilterResults();


                if (constraint!= null && constraint.toString().length() > 0) {

                SmithWaterman metric = new SmithWaterman();

               for(String element : all_mkey){
                   //La mappa dell'adapter è riempito con le sole entry simily all'occorrenza ricercata
                   if (metric.getSimilarity(constraint.toString().toLowerCase(), all_map_jid.get(element).getUserName().toLowerCase()) >= 0.8 ){

                       UserEntry rEntry=all_map_jid.get(element);

                       searched_user.put(element, rEntry );

                   }

               }

              results.values = searched_user;
              results.count = searched_user.size();

                }
                else{


                        results.values = all_map_jid;
                        results.count = all_map_jid.size();

                }

                return results;
            }
        };
    }
  • 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-06-15T21:50:23+00:00Added an answer on June 15, 2026 at 9:50 pm

    Solved

    I solved the issue, the problem was that I created a new Filter object on any new typing, using the same Filter all works well, because as is written in the android documentation:

    public final void filter (CharSequence constraint,
    Filter.FilterListener listener)

    Added in API level 1 Starts an asynchronous filtering operation.
    Calling this method cancels all previous non-executed filtering
    requests and posts a new filtering request that will be executed
    later.

    This is the correct code:

    //My Adapter 
    public class RosterAdapter extends BaseAdapter implements Filterable{
    
        //private ViewHolder holder;
        private LayoutInflater inflater;
    
        private ItemsFilter mFilter;
    
        private HashMap<String, UserEntry> mappa_users=null;
        private HashMap<String, UserEntry> all_map_jid=null;
        private ArrayList<String> all_mkey=null;;
        private ArrayList<String> mKeys;
    
    
        public RosterAdapter(){
            this.mappa_users =new HashMap<String, UserEntry>();
            this.inflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.all_map_jid=new HashMap<String, UserEntry>();
            this.all_mkey=new ArrayList<String>();
            this.mKeys=new ArrayList<String>();
        }
        @Override
        public int getCount() {
            return mappa_users.size();
        }
    
        @Override
        public UserEntry getItem(int position) {
            return mappa_users.get(mKeys.get(position));
        }
    
        @Override
        public long getItemId(int arg0) {
            return arg0;
        }
    
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
    
    
            ViewHolder holder;
    
    
            if (convertView == null) {
    
                convertView = inflater.inflate(R.layout.layout_row, null);
    
                // Creates a ViewHolder and store references to the two children
                // views we want to bind data to.
                holder = new ViewHolder();
                holder.username = (TextView) convertView.findViewById(R.id.user_info);
                holder.availability = (ImageView) convertView.findViewById(R.id.user_availability);
                holder.user_ic = (ImageView) convertView.findViewById(R.id.icon);
    
    
                // Keep track of the view holder as a tag of the view
                convertView.setTag(holder);
    
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag(); 
            }
    
    
    
            String user = getItem(position).getUserName();
            //Log.e("Nome","username "+user);
            holder.username.setText(user);
    
    
    
            if(!(getItem(position).getUserStatus())){
    
    
                holder.availability.setImageResource(R.drawable.ic_not_available);  
    
            }else{
    
                holder.availability.setImageResource(R.drawable.ic_available);
            }
    
            //do your view stuff here
    
            return convertView;
        }
    
    
    
    
    
        /**
         * Implementing the Filterable interface.
         */
        @Override
        public Filter getFilter() {
            if (mFilter == null) {
                mFilter = new ItemsFilter();
            }
            return mFilter;}
    
    
            /**
             * Custom Filter implementation for the items adapter.
             *
             */
            private class ItemsFilter extends Filter {
                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
    
                    adapter.mappa_users = (HashMap<String,UserEntry>) results.values;
                    adapter.mKeys= new ArrayList<String>(Arrays.asList(adapter.mappa_users.keySet().toArray(new String[adapter.mappa_users.size()])));
                    Collections.sort(mKeys, new RosterEntryComparator(adapter.mappa_users));
    
                    notifyDataSetChanged();
                    Log.e("fine", "Terminato filtraggio "+constraint);
    
                    }
    
    
                @Override
                protected  FilterResults performFiltering(CharSequence constraint) {
    
                    HashMap<String,UserEntry> searched_user=new HashMap<String,UserEntry>();
                    FilterResults results = new FilterResults();
    
    
                    if (constraint!= null && constraint.toString().length() > 0) {
    
                    SmithWaterman metric = new SmithWaterman();
    
                   for(String element : all_mkey){
                       //La mappa dell'adapter è riempito con le sole entry simily all'occorrenza ricercata
                       if (metric.getSimilarity(constraint.toString().toLowerCase(), all_map_jid.get(element).getUserName().toLowerCase()) >= 0.8 ){
    
                           UserEntry rEntry=all_map_jid.get(element);
    
                           searched_user.put(element, rEntry );
    
                       }
    
                   }
    
                  results.values = searched_user;
                  results.count = searched_user.size();
    
                    }
                    else{
    
    
                            results.values = all_map_jid;
                            results.count = all_map_jid.size();
    
                    }
    
                    return results;
                }
            };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with javascript code. I am using google maps and I am
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
Hi i have problem with this code, i found it on the internet and
i am a beginner and i have a problem : this code doesnt compile
I have a code problem which stems from the fact that I am using
I have a problem in this code when I enter a string, instead of
I have a problem with my code. How can I handle an error from
I have a problem with this code: (The error is below the code) public
i have a problem with my code. foreach (DataRow dr in dt_pattern.Rows) { part
I have some problem with my code public IQueryable<PageItems> GetPageById(Guid Id) { var xml

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.