0

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;
    }
DeNitE Appz
  • 1,003
  • 2
  • 12
  • 18
  • The ListView displays all of the information correctly... (with both of the suggestions below)... It's the validation that gets screwed up... it's like the *.equals() just stops working if I use the "convertView == null" method. (Basically I'm just changing the text color and ticking the correct answer)... with the convertView == null... several items tick... and several items turn red instead of just one. – DeNitE Appz Jun 07 '15 at 17:59
  • If I move the validation within the {...} it works, but then the list displays one item several times... – DeNitE Appz Jun 07 '15 at 18:00
  • OK, I solved the problem... I had to rest all of the check mark text colors back to white and untick them each time... Does that seem normal? – DeNitE Appz Jun 07 '15 at 19:38

2 Answers2

0
 convertView = theInflater.inflate(R.layout.score_layout, parent, false);

Initialize your convertView like this.. Is should work perfectly.

Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38
0
  1. Remove the final to the ViewHolder you do not need that.
  2. In the convertView == null add:

    convertView = theInflater.inflate(R.layout.score_layout, null);
    

    It should be much better.

Laurent Meyer
  • 2,766
  • 3
  • 33
  • 57