删除元素后的Arraylist IndexOutOfBoundsException

时间:2014-07-07 14:22:57

标签: java android listview arraylist

我知道这是一个愚蠢的问题,但我已经花了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());
                        }
                    });

行:questionList.remove(0)是问题,但为什么?列表中充满了元素,我只是想删除第一个元素,它怎么能被outofbound?我想,也许它会在我再次使用列表的代码中发生,也许我说某个地方我希望比我实际给出的更多的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

如果您还需要进一步的代码,我会添加评论。我不想打扰你。

EDIT(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;

    }

2 个答案:

答案 0 :(得分:2)

此处发生错误:

Question question = questionList.get(position);

position2且超出范围。您可能希望先检查或阻止该方法获得等于或大于position大小的questionList值。看来你的名单在U.I.有比questionList有问题

更多的项目

答案 1 :(得分:1)

在getView中给你的位置应该考虑数据的大小。

我的猜测是,当你创建适配器时,你会给它一个特定的列表(问题列表),但在稍后的某个时刻,你将在本地创建一个新的列表实例,在不更新适配器的情况下修改它。

确保您对列表所做的任何更改都在同一个列表实例上,并且不会创建新的列表实例。