0

I am trying to filter out results that do not start with the text entered in the autocompletetextview textbox. The initial hashmap gets loaded via an asynctask that runs the in background once the program loads. Here is my performFilter code:

   try{

                    // Note the clone - original list gets stripped
                    for(Iterator<Map.Entry<String, String>> it = dataToSearch.entrySet().iterator(); it.hasNext();)
                    {
                        Map.Entry<String, String> entry = it.next();
                        String lowercase = entry.getValue().toLowerCase();                           
if(lowercase.startsWith(constraint.toString().toLowerCase()))
                            searchMap.put(entry.getKey(), entry.getValue());
                            it.remove();

                    }

                }catch (Exception e){
                    Log.d("HUS","EXCEPTION "+e);
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = searchMap;
                filterResults.count = searchMap.size();

My issue is that either all of the results from the initial hashmap show up or none of the results show up. What might I be doing wrong? The async task calls the adapter.notifyDatasetChanged when the initial hashmap changes but I still would think that the filter results would be based on what the text in the textview starts with. Any help is appreciated.

Kyle Denney
  • 11
  • 1
  • 4
  • use [this](https://gist.github.com/pskink/2dd4d17a93caf02ff696533e82f952b0) generic adapter – pskink Aug 08 '16 at 18:59
  • Do you have a code example of how to implement the said adapter? What would be wrong about using the hashmap that I have above? – Kyle Denney Aug 08 '16 at 19:01
  • see the first comment, it shows the additional features – pskink Aug 08 '16 at 19:02
  • or you can skip the initial loading if the number of all items is huge and load the data as you type, more [here](http://stackoverflow.com/a/19860624/2252830) – pskink Aug 08 '16 at 19:04
  • I am trying to figure out the best way to implement the adapter and how to integrate it with my code. – Kyle Denney Aug 08 '16 at 19:12
  • you need just to implement `onBind` and `matches` methods, thats all – pskink Aug 08 '16 at 19:13
  • On the overload for the adapter what is 'textViewResourceId' - I tried putting the autocompletetextview in it like so: adapter = new MatchableArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, R.id.autoSearchMap); – Kyle Denney Aug 08 '16 at 19:24
  • Also, looking through the source code this is using an arraylist of type string but I am specifcally using a Hashmap of type . Would that be an issue? – Kyle Denney Aug 08 '16 at 19:29
  • it is not using any string, it uses any type object, just like ArrayAdaptet – pskink Aug 08 '16 at 19:32
  • I would like to move this conversation to a chat but I don't have enough reputation. I still am having questions around your source code. – Kyle Denney Aug 08 '16 at 19:40
  • see [this](http://pastebin.com/sVyJ08Fd), try to type `eso` for example and you will see multiple "peso" currencies – pskink Aug 09 '16 at 06:50
  • I was able to solve the issue. I moved the filter code from the onFilter method to the onTextChanged in the codebase and that is working well. Thank you so much for the feedback. – Kyle Denney Aug 09 '16 at 19:25

0 Answers0