索引超出范围自动完成的android [英] index out of bounds auto complete android

查看:67
本文介绍了索引超出范围自动完成的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中的自动完成功能。我动态设置适配器,但我得到一个IndexOutOfBoundsException异常,当我点击的建议的词汇之一。我不知道如何解决它。一些帮助将AP preciated。

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.

这里的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.

推荐答案

您code说:

如果输入的字符串长度为0 ...大写字母,然后检查这个词与它启动,但输入的字符串长度为0,为什么你会想这样做这两件事情?

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
}

修改

您应该改变你的方法输入变量名,以帮助您:

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.

这篇关于索引超出范围自动完成的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆