检查是否单击了setOnItemClickListener

时间:2016-05-30 17:09:16

标签: java android

我正在制作一本字典应用程序。我有一个AutoCompleteTextView和大型字符串列表。当我将ArrayAdapter设置为AutoCompleteTextView而不缩小它时,它会冻结。这就是我决定用TextWatcher设置Adapter的原因。 textView上的文本更改,textView适配器更改。但是当我尝试使用onitemClickListener选择所选项时,它会返回indexoutofboundsException,因为textWatcher在onclickListener和适配器更改之前工作。 有没有办法检查TextWatcher中是否单击了该项?谢谢你的帮助

final AutoCompleteTextView textView =  (AutoCompleteTextView) rootView.findViewById(R.id.autoCompleteTextView1);
    textView.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length() != 0){
                    db.openDataBase();
                     wl = db.getWordList(s.toString());
                    ArrayAdapter<String>  adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_dropdown_item_1line, wl.getWordList());
                    textView.setAdapter(adapter);
                }
            }


        textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                if(parent.getItemAtPosition(position) != null);
                    Log.i("Debug",parent.getItemAtPosition(position)+"");
                }


        });

这是错误 05-30 10:30:14.789: E/AndroidRuntime(10370): FATAL EXCEPTION: main 05-30 10:30:14.789: E/AndroidRuntime(10370): Process: com.alynurly.maralsozluk, PID: 10370 05-30 10:30:14.789: E/AndroidRuntime(10370): java.lang.IndexOutOfBoundsException: Invalid index 5, size is 4 05-30 10:30:14.789: E/AndroidRuntime(10370): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 05-30 10:30:14.789: E/AndroidRuntime(10370): at java.util.ArrayList.get(ArrayList.java:308) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.widget.AdapterView.getItemAtPosition(AdapterView.java:764) 05-30 10:30:14.789: E/AndroidRuntime(10370): at com.alynurly.maralsozluk.MainActivity$PlaceholderFragment$1.onItemClick(MainActivity.java:92) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:902) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1192) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.widget.AdapterView.performItemClick(AdapterView.java:299) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.widget.AbsListView.performItemClick(AbsListView.java:1113) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.widget.AbsListView$3.run(AbsListView.java:3638) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.os.Handler.handleCallback(Handler.java:733) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.os.Handler.dispatchMessage(Handler.java:95) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.os.Looper.loop(Looper.java:136) 05-30 10:30:14.789: E/AndroidRuntime(10370): at android.app.ActivityThread.main(ActivityThread.java:5021) 05-30 10:30:14.789: E/AndroidRuntime(10370): at java.lang.reflect.Method.invokeNative(Native Method) 05-30 10:30:14.789: E/AndroidRuntime(10370): at java.lang.reflect.Method.invoke(Method.java:515) 05-30 10:30:14.789: E/AndroidRuntime(10370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 05-30 10:30:14.789: E/AndroidRuntime(10370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 05-30 10:30:14.789: E/AndroidRuntime(10370): at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

感谢您的帮助。我了解到可以使用autoCompleteTextView.isPerformingOperation()检查是否点击了dropdownitem。所以现在我的新代码看起来像

    @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
  //doesn't work if list is clicked
            if(s.length() != 0 && !textView.isPerformingCompletion()){
                db.openDataBase();
                 wl = db.getWordList(s.toString());
                ArrayAdapter<String>  adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_dropdown_item_1line, wl.getWordList());
                textView.setAdapter(adapter);
            }
        }