-1

So I have a quiz app, in which I present the user with a question and four options. The 4 options are given in a RecyclerView, so I am passing the 4 options(as string ArrayList) and the correct answer(string) to the RecyclerView adapter constructor. Now if the chosen answer is correct the itemView is set to green and if wrong it is set to red(up to this is working fine).

My problem is that

when I press the wrong answer I have to set the background color of the itemview(already drawn) with correct answer to turn green along with the selected wrong answer turning red

See my code below

@Override
public void onBindViewHolder(final OptionsAdapter.ViewHolder viewHolder, final int i) {
    viewHolder.tv_name.setText(option.get(i));
    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (option.get(i).equals(correct)){
                Toast.makeText(context,"CORRECT ANSWER :)",Toast.LENGTH_SHORT).show();
                viewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.correctAnswer));
            } else {
                Toast.makeText(context,"SORRY INCORRECT ANSWER :(",Toast.LENGTH_SHORT).show();
                viewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.wrongAnswer));
            }
        }
    });

}

In the above snippet of my onBindViewHolder, the arraylist option is having the 4 options and the string correct is the correct answer.

How can i turn the correct answer to turn green when the wrong answer clicked?

Below image shows what iam expecting when wrong answer clicked

enter image description here

Maddy
  • 4,525
  • 4
  • 33
  • 53
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44

1 Answers1

4

Add These methods to your model class:

private boolean isSelected;

public boolean isSelected() {
    return isSelected;
}

public void setSelected(boolean selected) {
    isSelected = selected;
}

Add this code onClick on your adapter:

if (mArrayList.get(position).isSelected()) {
    mArrayList.get(position).setSelected(false);
} else {
    mArrayList.get(position).setSelected(true);
}
notifyItemChanged(position);
Maddy
  • 4,525
  • 4
  • 33
  • 53
Ronak Thakkar
  • 2,515
  • 6
  • 31
  • 45