【发布时间】:2014-08-28 01:42:17
【问题描述】:
我知道这是个愚蠢的问题,但我已经花了 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) 是问题所在,但为什么呢?列表中满是元素,我只想删除第一个,怎么会出站呢?我在想它可能会在稍后再次使用列表的代码中发生,也许我在某处说我期望的数组列表比我实际给出的更大。
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
如果您还需要更多代码,我会添加评论。我不想过多地向您发送垃圾邮件。
编辑(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;
}
【问题讨论】:
-
您的错误出现在 MyCustomAdapter.getView,而不是 questionList.remove。请发布此方法实现。
-
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2您在某处访问 2(通过变量或硬编码)。您发布的方法不是发生错误的代码。能发下相关代码吗? -
非常清晰的错误信息:
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2. -
既然你得到了所有的输出,这意味着它是从所有的代码中传递过来的。其他地方是错误。双击 logcat 行,其中之一将带您进入错误
-
问题已编辑,提前谢谢。
标签: java android listview arraylist