I am trying to create a mcq paper and fetching value from json in adapter radio buttons android want to store the value in array list i have two option true and false but when i click on true its adding the value but after clicking false the position is not getting replaced i want to store answer in array list
public class ExamsQuestionAdapter extends RecyclerView.Adapter<ExamsQuestionAdapter.ViewHolder> {
Context context;
List<Exam_Question_Fetching> arrayList;
int j=0;
ArrayList<String> selectedAnswer=null;
LayoutInflater inflter;
public ExamsQuestionAdapter(Context context, List<Exam_Question_Fetching> arrayList) {
this.context = context;
this.arrayList = arrayList;
arrayList=new ArrayList<>();
selectedAnswer = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
selectedAnswer.add("Not Attempted");
}
inflter = (LayoutInflater.from(context));
}
@NonNull
@Override
public ExamsQuestionAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.exam_question_layout,parent,false);
return new ExamsQuestionAdapter.ViewHolder(view);
}
public ArrayList<String> getCheckBoxValue(){
//initialize
return selectedAnswer;
}
@Override
public void onBindViewHolder(@NonNull final ExamsQuestionAdapter.ViewHolder holder, final int position) {
final Exam_Question_Fetching repo = arrayList.get(position);
holder.Question.setText(repo.getQuestion());
holder.buttontrue.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
try {
selectedAnswer.add(position, "True");
}
catch (IndexOutOfBoundsException e)
{
Toast.makeText(context,e.toString(),Toast.LENGTH_SHORT).show();
}
}
}
});
holder.buttonfalse.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
try {
selectedAnswer.add(position, "false");
}
catch (IndexOutOfBoundsException e)
{
Toast.makeText(context,e.toString(),Toast.LENGTH_SHORT).show();
}
}
}
});
holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup rg, int checkedId) {
for(int i=0; i<rg.getChildCount(); i++) {
RadioButton btn = (RadioButton) rg.getChildAt(i);
if(btn.getId() == checkedId)
{
//arrayList=new ArrayList<>();
//selectedAnswer=new ArrayList<>();
/*for (int j=0;j<arrayList.size();j++)
{
String text = btn.getText().toString();
selectedAnswer.set(j,text);
Toast.makeText(context,text,Toast.LENGTH_SHORT).show();
return;
}*/
}
}
}
});
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView Question,date,type,subname,marks,duration,totalquestion;
RadioButton buttontrue,buttonfalse;
RadioGroup group;
public ViewHolder(View itemView) {
super(itemView);
Question=itemView.findViewById(R.id.examQuestion);
buttonfalse=itemView.findViewById(R.id.radio_false);
buttontrue=itemView.findViewById(R.id.radio_true);
group=itemView.findViewById(R.id.radio_group);
}
}
}