-2

enter image description here

I populated a list of questions and answers with ListView.
Each question has 6 options(single selection); if I select any option, the previously selected option should be invalidated.

@Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        View views = convertView;
            views = inflater.inflate(R.layout.row_question_list, null);
            holder = new ViewHolders();
        holder.titleQuestion = (TextView)views.findViewById(R.id.tv_quetion_label);
        holder.c_option1 = (TextView)views.findViewById(R.id.question);

        holder.ibOption1= (ImageButton)views.findViewById(R.id.ibOption1);
        holder.ibOption2= (ImageButton)views.findViewById(R.id.ibOption2);
        holder.ibOption3= (ImageButton)views.findViewById(R.id.ibOption3);
        holder.ibOption4 = (ImageButton)views.findViewById(R.id.ibOption4);
        holder.ibOption5 = (ImageButton)views.findViewById(R.id.ibOption5);
        holder.ibOption6 = (ImageButton)views.findViewById(R.id.ibOption6);

  holder.ibOption6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // Need to select current selection and invalide previous selection.
// For current selection
 ((ImageButton) view).setSelected(type);
            }
        });
return views;
}

How to get the views of the other options?

Adinia
  • 3,722
  • 5
  • 40
  • 58
Sreekanth
  • 407
  • 4
  • 8
  • 20

1 Answers1

0

the simplest and better solution is to use Radio Group in your row_question_list with six radio buttons inside. You can further change orientation of parent radio group to horizontal to get above view. And can eventually get the selected radio button.

<RadioGroup>
   <RadioButton>
   <RadioButton>
   <RadioButton>
   <RadioButton>
   <RadioButton>
   <RadioButton>
</RadioGroup>

Further you can customize there backgrounds and set questions texts with them.

for the above view, you can make a drawable with images for state_selected = true and state_selected = false after every click on each button you can change button states of all the rest of buttons in the view or even you can change backgrounds.

holder.ibOption6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               holder.ibOption1.setBackgroundResource(your background);
               holder.ibOption2.setBackgroundResource(your background);
               holder.ibOption3.setBackgroundResource(your background);
               holder.ibOption4.setBackgroundResource(your background);
               holder.ibOption5.setBackgroundResource(your background);
            }
        });

and you will have to set this for all the buttons. or you can try notifyDataSetChanged();.

Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25