index out of bounds auto complete android
<p>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. </p>

<p><strong>Here's the code:</strong></p>

<pre><code>final ArrayList&lt;Person&gt; suggestions  = new ArrayList&lt;Person&gt;();
        final ArrayList&lt;Person&gt; 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&lt;Person&gt;(getApplicationContext() , R.layout.listitem , suggestions));
                }
                if(input.length() &gt; 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&lt;Person&gt;(getApplicationContext() , R.layout.listitem , suggestions));
                }

            }
        });

        autocomplete.setOnItemClickListener(new OnItemClickListener() {

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

<p>Thanks in advance.</p>
