简体   繁体   English

删除元素后的Arraylist IndexOutOfBoundsException

[英]Arraylist IndexOutOfBoundsException after removing element

I know it is dumb question but I already spent 1,5 hour by looking in the code and did not find anything: 我知道这是一个愚蠢的问题,但我已经花了1.5小时查看代码并没有找到任何东西:

holder.removeQuestion
                    .setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            // Button b = (Button) v;
                            // Question question = (Question) b.getTag();
                            Log.i("before removing", questionList.get(0)
                                    .getQuestion());
                            questionList.remove(0);
                            dataAdapter.notifyDataSetChanged();
                            Log.i("after removing", questionList.get(0)
                                    .getQuestion());
                        }
                    });

The line: questionList.remove(0) is the problem, but why? 行: questionList.remove(0)是问题,但为什么? The list is full of elements and I just want to remove the first one, how can it be outofbound? 列表中充满了元素,我只是想删除第一个元素,它怎么能被outofbound? I was thinking that maybe it happens later in code where I use the list again and maybe I am saying somewhere that I expect bigger arraylist than I actually give. 我想,也许它会在我再次使用列表的代码中发生,也许我说某个地方我希望比我实际给出的更多的arraylist。

07-07 13:24:08.005: I/before removing(16695): Ako?
07-07 13:24:08.005: I/after removing(16695): Sa?
07-07 13:24:08.009: V/ConvertView(16695): 0
07-07 13:24:08.009: V/ConvertView(16695): 1
07-07 13:24:08.013: V/ConvertView(16695): 2
07-07 13:24:08.017: D/AndroidRuntime(16695): Shutting down VM
07-07 13:24:08.017: W/dalvikvm(16695): threadid=1: thread exiting with uncaught     
exception (group=0xa62b1288)
07-07 13:24:08.021: E/AndroidRuntime(16695): FATAL EXCEPTION: main
07-07 13:24:08.021: E/AndroidRuntime(16695): java.lang.IndexOutOfBoundsException:     
Invalid index 2, size is 2
07-07 13:24:08.021: E/AndroidRuntime(16695):    at     
java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-07 13:24:08.021: E/AndroidRuntime(16695):    at 
java.util.ArrayList.get(ArrayList.java:304)
07-07 13:24:08.021: E/AndroidRuntime(16695):    at 
com.example.discue.Discussion$MyCustomAdapter.getView

If you need also further code just comment I will add. 如果你还需要进一步的代码,我会添加评论。 I did not want to overspam you. 我不想打扰你。

EDIT(onView method): 编辑(onView方法):

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) 
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.question_list, null);

            holder = new ViewHolder();
            holder.question = (TextView) convertView
                    .findViewById(R.id.question);
            holder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.likes = (TextView) convertView
                    .findViewById(R.id.numberOfLikes);
            // create button for deleting question (for speaker)
            holder.removeQuestion = (Button) convertView
                    .findViewById(R.id.deleteQuestionBtn);
            convertView.setTag(holder);
            // set onclick listener on remove question button
            holder.removeQuestion
                    .setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            // Button b = (Button) v;
                            // Question question = (Question) b.getTag();
                            Log.i("before removing", questionList.get(0)
                                    .getQuestion());
                            questionList.remove(0);
                            dataAdapter.notifyDataSetChanged();
                            Log.i("after removing", questionList.get(0)
                                    .getQuestion());
                        }
                    });
            // set onclick listener on like check box for question
            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    Question question = (Question) cb.getTag();
                    if (cb.isChecked()) {
                        // update number of likes under like button
                        question.setLikes(question.getLikes() + 1);

                    } else {
                        question.setLikes(question.getLikes() - 1);
                    }

                    // resort the arraylist of questions
                    Collections.sort(questionList,
                            new Comparator<Question>() {
                                public int compare(Question q1, Question q2) {
                                    return (q2.getLikes() - q1.getLikes());
                                }
                            });
                    // execute the update
                    dataAdapter.notifyDataSetChanged();

                    question.setSelected(cb.isChecked());
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Question question = questionList.get(position);
        // show/hide question remove button (for the first question)
        if (question.isRemoveButtonVisible()) {
            holder.removeQuestion.setVisibility(View.VISIBLE);
        } else {
            holder.removeQuestion.setVisibility(View.GONE);
        }
        // show/ hide question likes
        if (question.isLikeButtonVisible()) {
            holder.checkBox.setVisibility(View.VISIBLE);
        } else {
            holder.checkBox.setVisibility(View.GONE);
        }
        // set checked/unchecked and disabled/not disabled like check boxes
        // (all except for the first question)
        if (question.isLikeDisabled()) {
            holder.checkBox.setChecked(true);
            holder.checkBox.setEnabled(false);
        } else {
            holder.checkBox.setChecked(question.isSelected());
            holder.checkBox.setEnabled(true);

        }
        holder.question.setText(question.getQuestion());
        holder.likes.setText(question.getLikes() + "");
        holder.checkBox.setTag(question);
        holder.removeQuestion.setTag(question);
        dataAdapter.notifyDataSetChanged();
        return convertView;

    }

The error occurs here: 这里发生错误:

Question question = questionList.get(position);

The position is 2 and that is out of the bound. position2 ,超出界限。 You may want to do a check before or prevent the method from getting a position value that is equal to or larger than the size of questionList . 您可能希望先检查或阻止该方法获取等于或大于questionList大小的position值。 It seems your list in the UI has more items than the questionList has Questions 看来你的UI列表中的项目比questionList有问题的项目多

The position given to you in getView should take into account the size of the data. 在getView中给你的位置应该考虑数据的大小。

My guess is, when you create the adapter, you're giving it a certain list (the question list), but at some point later on, you're creating a new instance of the list locally and modifying it without updating the adapter. 我的猜测是,当你创建适配器时,你给它一个特定的列表(问题列表),但在稍后的某个时候,你在本地创建一个新的列表实例并在不更新适配器的情况下修改它。

Make sure any changes you make to the list are on the same list instance and no new list instance are created. 确保对列表所做的任何更改都在同一个列表实例上,并且不会创建新的列表实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM