I'm having a problem with my ListView adapter displaying items in my ArrayList...
For the most part, it seems to work... the view is correct for each item... however, the validation seems to get screwed up when I implement the (convertView == null)...
Without this... the displays work perfectly with all of the correct validation... but as soon as I put in the (convertView == null) section, none of my validation works correctly... It's the oddest thing.
Works perfectly...
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;
}
Validation screws up:
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;
}
Initialize your convertView like this.. Is should work perfectly.