我的ListView适配器显示我的ArrayList中的项目有问题...
在大多数情况下,它似乎有效...每个项目的视图都是正确的......但是,当我实现(convertView == null)时,验证似乎搞砸了......
没有这个......显示器可以完美地运行所有正确的验证...但是只要我放入(convertView == null)部分,我的验证都没有正常工作......这是最奇怪的事情
完美运作......
public View getView(int position, View convertView, ViewGroup parent)
{
// View rowView = convertView;
final ViewHolder viewHolder;
LayoutInflater theInflater = LayoutInflater.from(context);
convertView = theInflater.inflate(R.layout.score_layout, null);
viewHolder = new ViewHolder();
viewHolder.textViewScoreQuestion = (TextView) convertView.findViewById(R.id.textViewScoreQuestion);
viewHolder.checkBoxScoreOne = (CheckBox) convertView.findViewById(R.id.checkBoxScoreOne);
viewHolder.checkBoxScoreTwo = (CheckBox) convertView.findViewById(R.id.checkBoxScoreTwo);
viewHolder.checkBoxScoreThree = (CheckBox) convertView.findViewById(R.id.checkBoxScoreThree);
viewHolder.checkBoxScoreFour = (CheckBox) convertView.findViewById(R.id.checkBoxScoreFour);
Questions question = questionsInOrder.get(position);
String givenAnswer = answerChoices.get(position);
viewHolder.textViewScoreQuestion.setText(question.getQuestion());
viewHolder.checkBoxScoreOne.setText(question.getAnswer1());
viewHolder.checkBoxScoreTwo.setText(question.getAnswer2());
viewHolder.checkBoxScoreThree.setText(question.getAnswer3());
viewHolder.checkBoxScoreFour.setText(question.getAnswer4());
// display incorrect answers as red, and correct answers as blue
if (givenAnswer.equals(question.getAnswer1()) && !(givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreOne.setTextColor(context.getResources().getColor(R.color.red));
}
if (givenAnswer.equals(question.getAnswer1()) && (givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreOne.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
}
if (givenAnswer.equals(question.getAnswer2()) && !(givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreTwo.setTextColor(context.getResources().getColor(R.color.red));
}
if (givenAnswer.equals(question.getAnswer2()) && (givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreTwo.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
}
if (givenAnswer.equals(question.getAnswer3()) && !(givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreThree.setTextColor(context.getResources().getColor(R.color.red));
}
if (givenAnswer.equals(question.getAnswer3()) && (givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreThree.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
}
if (givenAnswer.equals(question.getAnswer4()) && !(givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreFour.setTextColor(context.getResources().getColor(R.color.red));
}
if (givenAnswer.equals(question.getAnswer4()) && (givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreFour.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
}
// check all of the correct answers
if (question.getAnswer1().equals(question.getCorrect()))
{
viewHolder.checkBoxScoreOne.setChecked(true);
}
if (question.getAnswer2().equals(question.getCorrect()))
{
viewHolder.checkBoxScoreTwo.setChecked(true);
}
if (question.getAnswer3().equals(question.getCorrect()))
{
viewHolder.checkBoxScoreThree.setChecked(true);
}
if (question.getAnswer4().equals(question.getCorrect()))
{
viewHolder.checkBoxScoreFour.setChecked(true);
}
return convertView;
}
验证搞砸了:
public View getView(int position, View convertView, ViewGroup parent)
{
// View rowView = convertView;
final ViewHolder viewHolder;
if (convertView == null)
{
LayoutInflater theInflater = LayoutInflater.from(context);
convertView = theInflater.inflate(R.layout.score_layout, null);
viewHolder = new ViewHolder();
viewHolder.textViewScoreQuestion = (TextView) convertView.findViewById(R.id.textViewScoreQuestion);
viewHolder.checkBoxScoreOne = (CheckBox) convertView.findViewById(R.id.checkBoxScoreOne);
viewHolder.checkBoxScoreTwo = (CheckBox) convertView.findViewById(R.id.checkBoxScoreTwo);
viewHolder.checkBoxScoreThree = (CheckBox) convertView.findViewById(R.id.checkBoxScoreThree);
viewHolder.checkBoxScoreFour = (CheckBox) convertView.findViewById(R.id.checkBoxScoreFour);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
Questions question = questionsInOrder.get(position);
String givenAnswer = answerChoices.get(position);
viewHolder.textViewScoreQuestion.setText(question.getQuestion());
viewHolder.checkBoxScoreOne.setText(question.getAnswer1());
viewHolder.checkBoxScoreTwo.setText(question.getAnswer2());
viewHolder.checkBoxScoreThree.setText(question.getAnswer3());
viewHolder.checkBoxScoreFour.setText(question.getAnswer4());
// display incorrect answers as red, and correct answers as blue
if (givenAnswer.equals(question.getAnswer1()) && !(givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreOne.setTextColor(context.getResources().getColor(R.color.red));
}
if (givenAnswer.equals(question.getAnswer1()) && (givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreOne.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
}
if (givenAnswer.equals(question.getAnswer2()) && !(givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreTwo.setTextColor(context.getResources().getColor(R.color.red));
}
if (givenAnswer.equals(question.getAnswer2()) && (givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreTwo.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
}
if (givenAnswer.equals(question.getAnswer3()) && !(givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreThree.setTextColor(context.getResources().getColor(R.color.red));
}
if (givenAnswer.equals(question.getAnswer3()) && (givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreThree.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
}
if (givenAnswer.equals(question.getAnswer4()) && !(givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreFour.setTextColor(context.getResources().getColor(R.color.red));
}
if (givenAnswer.equals(question.getAnswer4()) && (givenAnswer.equals(question.getCorrect())))
{
viewHolder.checkBoxScoreFour.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
}
// check all of the correct answers
if (question.getAnswer1().equals(question.getCorrect()))
{
viewHolder.checkBoxScoreOne.setChecked(true);
}
if (question.getAnswer2().equals(question.getCorrect()))
{
viewHolder.checkBoxScoreTwo.setChecked(true);
}
if (question.getAnswer3().equals(question.getCorrect()))
{
viewHolder.checkBoxScoreThree.setChecked(true);
}
if (question.getAnswer4().equals(question.getCorrect()))
{
viewHolder.checkBoxScoreFour.setChecked(true);
}
return convertView;
}
答案 0 :(得分:0)
convertView = theInflater.inflate(R.layout.score_layout, parent, false);
像这样初始化你的convertView ..应该可以正常工作。
答案 1 :(得分:0)
final
移至您不需要的ViewHolder
。在convertView == null
添加:
convertView = theInflater.inflate(R.layout.score_layout, null);
应该好多了。