0

I created an HashMap<String, String> that i populated beforehand.

And i want my AutoCompleteTextView to tell me, as i'm typing if it's in the HashMap or not. Here's my code so far :

autocomplete.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(s.length() > 0){     
            if (map_clients.containsKey(s)){
                Toast.makeText(context, "He's here", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(context, "He's not here", Toast.LENGTH_SHORT).show();
            }
        }
        else {

        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {            

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

It always tell me "He's not here" even when i type the text fully or when i select it from the autocompletion.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
Tsunaze
  • 3,204
  • 7
  • 44
  • 81
  • 2
    You know that a HashMap will only find the key when it is written in full, don't you? If your key is "Hello", typing "He" will result in "not found" (also it is case sensitive so "hello" won't work, either). I think you are using the wrong data structure. – SJuan76 Apr 05 '13 at 10:34
  • But even when the word is written in full, it doesn't work, and even when i select the text from autocompletion, it didn't work. – Tsunaze Apr 05 '13 at 10:37
  • Unless you pass the full text to map_clients.containsKey(s), contains method is going to return FALSE only. – Jayamohan Apr 05 '13 at 10:37
  • If you check it with the complete, correctly cased word, then the error should be somewhere else. I would a) write in the toast the value in S, just in case the API is not returning what expected and b) write a small program that initializes your map and immediately checks the `containsKey` function (are you using keys that are not Strings?) – SJuan76 Apr 05 '13 at 10:43
  • It's working now, because i didn't use s, but s.toString(), that's because my HashMap is – Tsunaze Apr 05 '13 at 11:00

2 Answers2

2

You need to use a TreeMap to store your data, then it is sorted, and from there you can look for the best fit from your code.

Your other option is to use a SortedList or array and use binarySearch implementations for speed.

Chris Cooper
  • 4,982
  • 1
  • 17
  • 27
0
autocomplete.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(s.length() > 0){
                text = s.toString();
                if(map_clients.containsKey(text)){
                    Toast.makeText(context, "He's here", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(context, "He's not here", Toast.LENGTH_SHORT).show();
                }
            }else{

            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

I've changed it to text = s.toString(); I think it's because i put my HashMap in < String, String > and not CharSequence.

Tsunaze
  • 3,204
  • 7
  • 44
  • 81