发送带有改进的对象数组

问题描述

我想通过改装发送此数据

{"task_id" : "9","answers" : [
{"question_id": "1","answer": "true"},{"question_id": "2",{"question_id": "3","answer": "false"}
]}

我从选中的复选框发送数据 它仅在发送1个数据时有效,但是当我勾选另一个复选框时,它将返回错误数组索引,我将recyclerview用作复选框,这是我的适配器代码

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,int position) {
    final AdapterChecklistRelokasiVsat.ChecklistViewHolder myViewHolder = (AdapterChecklistRelokasiVsat.ChecklistViewHolder) holder;
    if (position == dataList.size()){
        myViewHolder.btSubmitChecklist.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                update("10");
            }
        });
    }else{
        final ModelRelokasiChecklist relokasiChecklist = dataList.get(position);
        myViewHolder.tvQuestion.setText(relokasiChecklist.getPertanyaan());
        myViewHolder.rb1.setChecked(relokasiChecklist.isCheck());
        myViewHolder.rb1.setonCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if (buttonView == null || !buttonView.ispressed()){
                    answers.remove(position);
                    return;
                }else{
                    System.out.println(buttonView.getText() + " "+position);
                    myViewHolder.rb2.setChecked(false);
                    relokasiChecklist.setCheck2(false);
                    answers = new ArrayList<>();
                    sends = new ArrayList<>();
                    if (buttonView.isChecked() == true){
                        for (int i = 0; i< dataList.size(); i++){
                            Answers ans = new Answers();
                            ans.setAnswer("true");
                            ans.setQuestion_id(dataList.get(position).getId());
                            answers.add(position,ans);
                            Send send = new Send();
                            send.setTaskId("9");
                            send.setAnswers(answers);
                        }
                    }
                }
                Log.d(TAG,"onCheckedChanged: buttonView:" + buttonView);
                relokasiChecklist.setCheck(isChecked);
            }
        });
        
    }
}





@POST("api/relocation/checklist/answers/update")
Call<ModelUpdatePresurveyResponse>updateChecklist(@Body SendBody object);

我这样使用类主体

public class SendBody {
@Serializedname("task_id")
public String taskId;

@Serializedname("answers")
public List<Answers> answers;

public SendBody(String taskId,List<Answers> answers) {
    this.taskId = taskId;
    this.answers = answers;
}


public class Answers {
@Serializedname("question_id")
public String question_id;
@Serializedname("answer")
public String answer;
public boolean isChecked;

public String getQuestion_id() {
    return question_id;
}

public void setQuestion_id(String question_id) {
    this.question_id = question_id;
}

public String getAnswer() {
    return answer;
}

public void setAnswer(String answer) {
    this.answer = answer;
}}

我知道我的错误是当我尝试向主体中插入值时,只有1个值被存储,而当我尝试插入更多内容时,我得到了错误数组错误

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)