2

I am using custom ListView with 2 TextView and four RadioButtons in a radio group. When I selected a RadioButton I want to show that radio group to get invisible and answer TextView gets visible. Its working perfectly but problem is when I scroll down and again scroll up the radio group gets visible by itself.

class CustomAdapter extends ArrayAdapter<String> {

    public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> objects) {
        super(context, resource, textViewResourceId, objects);

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View v = inflater.inflate(R.layout.row, parent, false);
        TextView mcqsText = (TextView) v.findViewById(R.id.mcqsText);
        final TextView ans2 = (TextView) v.findViewById(R.id.answer2);
        final TextView anss = (TextView) v.findViewById(R.id.answer);
        final RadioGroup rg = (RadioGroup) v.findViewById(R.id.radioGroup);
        RadioButton opt1 = (RadioButton) v.findViewById(R.id.optA);
        RadioButton opt2 = (RadioButton) v.findViewById(R.id.optB);
        RadioButton opt3 = (RadioButton) v.findViewById(R.id.optC);
        RadioButton opt4 = (RadioButton) v.findViewById(R.id.optD);
        final ImageView ivcorrect= (ImageView) v.findViewById(R.id.ivcorrect);
        final ImageView ivwrong= (ImageView) v.findViewById(R.id.ivwrong);

        mcqsText.setText(mcqs.get(position));
        opt1.setText(optA.get(position));
        opt2.setText(optB.get(position));
        opt3.setText(optC.get(position));
        opt4.setText(optD.get(position));
        anss.setText(ans.get(position));
        ans2.setText("Correct answer is = "+ans.get(position));

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int radioButtonID = group.getCheckedRadioButtonId();
                View radioButton = group.findViewById(radioButtonID);
                //find radiobutton position
                int position = group.indexOfChild(radioButton);
                RadioButton btn = (RadioButton) rg.getChildAt(position);
                String selection = (String) btn.getText();

                if (selection.equals(anss.getText().toString())) {
                    rg.setVisibility(View.GONE);
                    ans2.setVisibility(View.VISIBLE);
                    ivcorrect.setVisibility(View.VISIBLE);
                    correct++;
                } else {
                    ivwrong.setVisibility(View.VISIBLE);
                    ans2.setVisibility(View.VISIBLE);
                    wrong++;
                }

            }
        });

        return v;
    }
}
Paul
  • 4,160
  • 3
  • 30
  • 56
ASIF RAZA
  • 185
  • 1
  • 12

4 Answers4

1

Here is my solution

public class CurrentAffairs extends AppCompatActivity {
public static int correct, wrong, marks;
DbH db;
ArrayList<Question> mcqs;
Cursor cur;
ListView lv;
CustomAdapter c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.current_affairs);
    lv = (ListView) findViewById(R.id.lv);
    mcqs = new ArrayList<>();

    try {
        db = new DbH(this);
    } catch (IOException e2) {
        e2.printStackTrace();
    }
    try {
        db.createdatabase();
    } catch (IOException e) {
        e.printStackTrace();
    }
    db.opendatabase();
    cur = db.data();
    try {
        while (cur.moveToNext()) {
            String mcqss = cur.getString(1);
            String opta = cur.getString(2);
            String optb = cur.getString(3);
            String optc = cur.getString(4);
            String optd = cur.getString(5);
            String answ = cur.getString(6);

            Question question = new Question();
            question.question = mcqss;
            question.option1 = opta;
            question.option2 = optb;
            question.option3 = optc;
            question.option4 = optd;
            question.correctanxer = answ;
            mcqs.add(question);


           c = new CustomAdapter(CurrentAffairs.this, R.layout.row,    R.id.mcqsText, mcqs);
            lv.setAdapter(c);
        }
    } finally {
        cur.close();
    }
    Button btnshow = (Button) findViewById(R.id.btnshowresult);
    btnshow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            StringBuffer sb = new StringBuffer();
            sb.append("Correct Answer = " + " " + correct);
            sb.append("    " + "Wrong Answer = " + " " + wrong);
            sb.append("    " + "Final Score = " + " " + correct * 5);

            Toast.makeText(CurrentAffairs.this, sb, Toast.LENGTH_SHORT).show();
        }
    });
}



class CustomAdapter extends ArrayAdapter<Question> {

    public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<Question> objects) {
        super(context, resource, textViewResourceId, objects);


    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.row, parent, false);
        TextView mcqsText = (TextView) v.findViewById(R.id.mcqsText);
        TextView ans2 = (TextView) v.findViewById(R.id.answer2);
        TextView anss = (TextView) v.findViewById(R.id.answer);
        RadioGroup rg = (RadioGroup) v.findViewById(R.id.radioGroup);

        RadioButton opt1 = (RadioButton) v.findViewById(R.id.optA);
        RadioButton opt2 = (RadioButton) v.findViewById(R.id.optB);
        RadioButton opt3 = (RadioButton) v.findViewById(R.id.optC);
        RadioButton opt4 = (RadioButton) v.findViewById(R.id.optD);

        Question question = mcqs.get(position);

        mcqsText.setText(question.question);
        opt1.setText(question.option1);
        opt2.setText(question.option2);
        opt3.setText(question.option3);
        opt4.setText(question.option4);
        anss.setText(question.selectedanxer);
        ans2.setText("Correct answer is = " + question.correctanxer);

        String test=opt1.getText().toString();
        String test2=question.selectedanxer+"";

        if (test.equals(""+test2)){
            opt1.setChecked(true);
        }
        if (test2.equals(opt2.getText()+"")){
            opt2.setChecked(true);
        }
        if (test2.equals(opt3.getText()+"")){
            opt3.setChecked(true);
        }
        if (test2.equals(opt4.getText()+"")){
            opt4.setChecked(true);
        }




        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {


                Question question1=mcqs.get(position);
                RadioButton radioButton= (RadioButton) group.findViewById(checkedId);
                mcqs.get(position).selectedanxer=radioButton.getText().toString();
                notifyDataSetChanged();



            }
        });

        return v;
    }
}

public class Question {
    String question;
    String option1;
    String option2;
    String option3;
    String option4;

    String selectedanxer;
    String correctanxer;

}

}
Prashanth
  • 993
  • 8
  • 18
  • Please don't add the [same answer to multiple questions](http://meta.stackexchange.com/questions/104227/is-it-acceptable-to-ad??d-a-duplicate-answer-to-several-questions). Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, tailor the post to the question. – Manfred Radlwimmer Feb 22 '17 at 09:30
  • @ManfredRadlwimmer got it – Xäiñ Ul Abideen Feb 22 '17 at 09:48
0

Add in your class an int[] 'checked' array,

int[] checked;


public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> objects) {
        super(context, resource, textViewResourceId, objects);
        checked = new int[objects.size()];
        for(int curInt : checked)
             curInt = -1; // -1 as a flag, that none is checked yet for this item.
    }

in the getView method-

switch(checked[position]){
     case 0:
          //check your first radiobutton
          opt1.setChecked(true);
          rg.setVisibility(View.GONE);
          break;
     case 1:
          //check your second radiobutton
          opt2.setChecked(true);
          rg.setVisibility(View.GONE);
          break;
     case 2:
          //check your second radiobutton
          opt3.setChecked(true);
          rg.setVisibility(View.GONE);
          break;
     case 3:
          //check your second radiobutton
          opt4.setChecked(true);
          rg.setVisibility(View.GONE);
          break;
     default:
          break;
}

inside the onCheckedChanged listener, you check which is selected each time and update the 'checked' array-

switch(checkedId){
     case R.id.optA:
          checked[position] = 0;
          break;
     case R.id.optB:
          checked[position] = 1;    
          break;
     case R.id.optC:
          checked[position] = 2;
          break;
     case R.id.optD:
          checked[position] = 3;
          break;
}

I hope it helps :)

Matan Koby
  • 232
  • 1
  • 11
  • i write switch instead of select. and it is not solving the problum. plz help. – ASIF RAZA Feb 16 '17 at 13:17
  • You are right, switch instead of select, my bad. Im correcting my code and adding what you need for the rg to disappear. – Matan Koby Feb 16 '17 at 13:23
  • I added code to the getView switch-select, so everytime it calls an item which was already answered - it'll .setVisibility(View.GONE) the radiogroup. – Matan Koby Feb 16 '17 at 13:30
  • i want to show radio group visible in start and answer is invisible. when click on any radio button the radio group get invisible and answer get visible. that is working perfectly. but problem is when i scroll down and then i go up the radio group is visible by itself which i dont want to visible. i need solution for that. – ASIF RAZA Feb 16 '17 at 16:33
  • my problem is when i select radio buttons and scroll down list view and again i scroll up the selected fields deselected by itself. i try your code but its not working. plz if you can do any help. i am stucked here from four days. – ASIF RAZA Feb 17 '17 at 18:12
0

I had the same problem. but I solved it using the code below (all codes are in my ListView Adaptor): I defined an ArrayList that saved my selections:

 selectedAnswers = new ArrayList<>();

then I put listener for my RadioButton that saved selected items index in ArrayList:

RadioButton1.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                selectedAnswers.set( i, "0" );
        }
    } );

In the end, I defined a method that is called before the layout is returned

and I sent the position to this method:

   private void My_method(int i) {
    for (int j=0;j<=selectedAnswers.size();j++)
    {
        if (i == j) {

            int a = Integer.parseInt( selectedAnswers.get( i ).trim() );
            ((RadioButton) rg.getChildAt( a )).setChecked( true );
        }
    }
}
Jayendran
  • 9,638
  • 8
  • 60
  • 103
0

Your List view should be like this:

public class QuestionListAdaptor extends BaseAdapter {

public static int count1;
Context mContext;
public static ArrayList<String> selectedAnswers;
RadioGroup Radiogroup ;
public QuestionListAdaptor(Context mContext) {
    this.mContext = mContext;
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < count1; i++) {
        selectedAnswers.add( "4" );
    }
}

@Override
public int getCount() {
    return names.length;
}

@Override
public Object getItem(int i) {
    return names[i];
}

@Override
public long getItemId(int i) {
    return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    View row = LayoutInflater.from( mContext )
            .inflate( R.layout.question_list_item, viewGroup, false );

    RadioButton r1 = row.findViewById( R.id.RadioButton1 );
    RadioButton r2 = row.findViewById( R.id.RadioButton2 );
    RadioButton r3 = row.findViewById( R.id.RadioButton3 );
    RadioButton r4 = row.findViewById( R.id.RadioButton4 );

    r1.setText( "answer1 " );
    r2.setText( "answer2 "  );
    r3.setText( "answer3 "  );
    r4.setText( "answer4 "  );
    Radiogroup = row.findViewById( R.id.ReadioButton1 );




    r1.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                selectedAnswers.set( i, "0" );


        }
    } );
    r2.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                selectedAnswers.set( i, "1" );
        }
    } );
    r3.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                selectedAnswers.set( i, "2" );
            }
        }
    } );
    r4.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                selectedAnswers.set( i, "3" );
            }
        }
    } );
    method(i);
    return row;
}
private void method(int i) {
    for (int j=0;j<=selectedAnswers.size();j++)
    {
        if (i == j) {
            int a = Integer.parseInt( selectedAnswers.get( i ).trim() );
            ((RadioButton) Radiogroup .getChildAt( a )).setChecked( true );
    }
    }
}
}

Hope it helps.

CKE
  • 1,533
  • 19
  • 18
  • 29