简体   繁体   中英

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:

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? The list is full of elements and I just want to remove the first one, how can it be 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.

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):

@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. 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 . It seems your list in the UI has more items than the questionList has Questions

The position given to you in getView should take into account the size of the data.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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