Java 我试图创建一个mcq文件,并在android适配器单选按钮中从json获取值
我试图创建一个mcq文件,并在适配器单选按钮中从json获取值android想要在数组列表中存储值我有两个选项true和false,但当我单击true时,它会添加值,但单击false后,位置不会被替换 我想把答案存储在数组列表中Java 我试图创建一个mcq文件,并在android适配器单选按钮中从json获取值,java,android,Java,Android,我试图创建一个mcq文件,并在适配器单选按钮中从json获取值android想要在数组列表中存储值我有两个选项true和false,但当我单击true时,它会添加值,但单击false后,位置不会被替换 我想把答案存储在数组列表中 public class ExamsQuestionAdapter extends RecyclerView.Adapter<ExamsQuestionAdapter.ViewHolder> { Context context; 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);
}
}
}
公共类ExamsQuestionAdapter扩展了RecyclerView.Adapter{
语境;
列表数组列表;
int j=0;
ArrayList selectedAnswer=null;
更平坦的过滤器;
公共ExamsQuestionAdapter(上下文,列表arrayList){
this.context=上下文;
this.arrayList=arrayList;
arrayList=新的arrayList();
selectedAnswer=newarraylist();
对于(int i=0;i 对于(int i=0;i尝试使用hashmap存储答案。如果状态从true更改为False,如何替换首先从列表中获取位置(arraylist/hashmap),然后用您的答案替换相应的位置(已更新)。我尝试了此方法,我正在获取索引越界异常索引0大小0