lundi 12 mars 2018

Radio Button not selected when scrool in listview in android

When i scroll down in android list view , Radio Button can't selected . i apply set selected true on set change check listener , not working , need help. thanks advance. I use a custom adapter that used 4 radio button and set mcq answer option . 50 mcq but when scrolling in listview than not previous item selected . auto refresh . can any one suggest me what can i do

public class ModeltestDetailsAdapter extends ArrayAdapter {

private final Context context;
private final List<AllData> values;
public static ArrayList<String> selectedAnswers;

public ModeltestDetailsAdapter(Context context, List<AllData> values) {

    super(context, R.layout.list_details_modeltest, values);
    this.context = context;
    this.values = values;

    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < values.size(); i++) {
        selectedAnswers.add("Not Attempted");
    }

}

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

    // urveyDataHelper mDbHelper = new SurveyDataHelper(context);

    final ViewGroup p;
    p = parent;

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.list_details_modeltest, parent, false);

    TextView questions = (TextView) rowView.findViewById(R.id.questions);
    TextView answer = (TextView) rowView.findViewById(R.id.ans);
    final RadioGroup radioGroup = (RadioGroup) rowView.findViewById(R.id.radiogroup);
    RadioButton radioButton;
    String result;
    final RadioButton optiona= (RadioButton) rowView.findViewById(R.id.optiona);
    final RadioButton   optionb= (RadioButton) rowView.findViewById(R.id.optionb);
    RadioButton optionc= (RadioButton) rowView.findViewById(R.id.optionc);
    RadioButton optiond= (RadioButton) rowView.findViewById(R.id.optiond);

    questions.setText(values.get(position).getQues());
    answer.setText(values.get(position).getAns());
    optiona.setText(values.get(position).getOpt_one());
    optionb.setText(values.get(position).getOpt_two());
    optionc.setText(values.get(position).getOpt_three());
    optiond.setText(values.get(position).getOpt_four());

    optionc.setChecked(true);



    optiona.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // set Yes values in ArrayList if RadioButton is checked
            if (isChecked) {
                optiona.setChecked(true);

                //notifyDataSetChanged();
                Toast.makeText(
                        context,
                        "Answer is ." + buttonView.getText().toString(),
                        Toast.LENGTH_LONG).show();

                if (values.get(position).getAns().equals(buttonView.getText().toString())) {

                    selectedAnswers.set(position, "Correct");

                } else {
                    selectedAnswers.set(position, "Wrong");
                }

            }else {
                optiona.setChecked(false);
            }


        }
    });



    optionb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set Yes values in ArrayList if RadioButton is checked
            if (isChecked) {

                optionb.setChecked(true);

                // notifyDataSetChanged();
                Toast.makeText(
                        context,
                        "Answer is ." + buttonView.getText().toString(),
                        Toast.LENGTH_LONG).show();

                if (values.get(position).getAns().equals(buttonView.getText().toString())) {

                    selectedAnswers.set(position, "Correct");

                } else {
                    selectedAnswers.set(position, "Wrong");
                }

            }
            else {
                optionb.setChecked(false);
            }
        }

    });



    questions.setTag(position);
    answer.setTag(position);
    optiona.setTag(position);
    optionb.setTag(position);
    optionc.setTag(position);
    optiond.setTag(position);
    //radioButton.setTag(position);
    optiona.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final int pos = (Integer) v.getTag();

            optiona.setChecked(true);
            optiona.setSelected(true);

            ((ListView) p).performItemClick(v, pos, 0);
        }
    });


    answer.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final int pos = (Integer) v.getTag();

            ((ListView) p).performItemClick(v, pos, 0);
        }
    });

    questions.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final int pos = (Integer) v.getTag();

            ((ListView) p).performItemClick(v, pos, 0);
        }
    });

    return rowView;
}

}




Aucun commentaire:

Enregistrer un commentaire