开发者

index out of bounds auto complete android

i have an autocomplete function in my application. I set the adapter dynamically but i get an IndexOutOfBoundsException when i click one of the suggested words. I don't know how to solve it. Some help will be appreciated.

Here's the code:

final ArrayList<Person> suggestions  = new ArrayList<Person>();
        final ArrayList<Person> allPersons = database.selectPersons();

        autocomplete = (AutoCompleteTextView)findViewById(R.id.autocomplete);
        autocomplete.setThreshold(1);
        autocomplete.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

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

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

                input = autocomplete.getText().toString();

                if(input.length() == 0){
                    suggestions.clear();
                    input.toUpperCase();
                    for (Person p : allPersons) {
                        if(p.getName().startsWith(input)){
                            suggestions.add(p);

                        }
                    }
                    autocomplete.setAdapter(new ArrayAdapter<Person>(getApplicationContext() , R.layout.listitem , suggestions));
                }
                if(input.length() > 0){
                   开发者_高级运维 suggestions.clear();
                    input = Character.toUpperCase(input.charAt(0)) + input.substring(1);;
                    for (Person p : allPersons) {
                        if(p.getName().startsWith(input)){
                            suggestions.add(p);

                        }
                    }
                    autocomplete.setAdapter(new ArrayAdapter<Person>(getApplicationContext() , R.layout.listitem , suggestions));
                }

            }
        });

        autocomplete.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Person input = (Person)arg0.getItemAtPosition(arg2);
                Log.d("The person name is",input.getName());
            }
        });

Thanks in advance.


Your code says,

if the input string length is 0 ... uppercase it and then check that the word starts with it, but the input string is length 0 so why would you want to do these two things?

The first thing you should have at the top of your method is:

input = autocomplete.getText().toString();

if(input == null || input.isEmpty()) {
      return; // user hasn't done anything or has cleared the box
}

EDIT

You should change your method input variable names to help you:

 onItemClick(AdapterView<?> parent, View view, int position, long id);

Your array index out of bounds is in here, you need to check what your casting and the list that you are doing a get on. Ensure it holds your items.


So i found the answer. The thing is that i wanted to provide suggestions as the user types a word. Every time you type a letter you go into the onTextChanged() method and the list with suggestions gets erased and filled up with new suggestions. The trick is that when the user chooses a word from the suggestions you go into onTextChanged() method to and the suggestions list is filled up with the word you choosed. So what i did was to change the Person input = (Person)arg0.getItemAtPosition(arg2); to Person input = (Person)suggestions.get(0); which would be the word the user selected. I did some cleaning in the code as well inspired by @Blundell. I hope this helps someone. Thanks for the help.

Here's is the final code:

final ArrayList<Person> suggestions  = new ArrayList<Person>();
final ArrayList<Person> allPersons = database.selectPersons();

autocomplete = (AutoCompleteTextView)findViewById(R.id.autocomplete);
autocomplete.setThreshold(1);
autocomplete.addTextChangedListener(new TextWatcher() {

public void afterTextChanged(Editable s) {
}

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

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

    input = autocomplete.getText().toString();

    if(input.length() > 0){
       suggestions.clear();
       input = Character.toUpperCase(input.charAt(0)) + input.substring(1);;
       for (Person p : allPersons) {
           if(p.getName().startsWith(input)){
               suggestions.add(p);
           }
       }
       autocomplete.setAdapter(new ArrayAdapter<Person>(getApplicationContext() , R.layout.listitem , suggestions));
       }

    }
});

autocomplete.setOnItemClickListener(new OnItemClickListener() {

     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    Person input = suggestions.get(0);
                    Log.d("The person name is",input.getName());
                }
            });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜