Recyclerview 选择、判断题 的存储数值 是否为空 SparseBooleanArray的使用

//题目类型     1选择题   3判断题         4填空题
private final int CHOICE = 1;
private final int JUDGE = 3;
private final int FILL = 4;
private SparseBooleanArray mSelectedPositions = new SparseBooleanArray(); //存储是否选中

private String selected;//选中的文本
private String courseid;//单个题目id
private List courseidList = new ArrayList<>(); //题目id
private List mList = new ArrayList<>();

private String answer = "";
private String answers = "";
private String left = "\"";
private String right = "\"";

/**
 * Same as QuickAdapter#QuickAdapter(Context,int) but with
 * some initialization data.
 *
 * @param data A new list is created out of this one to avoid mutable list
 */
public ExampaperDetailsExampaperDetails(List data) {
    super(data);
    addItemType(CHOICE, R.layout.activity_test_question_item_choice);
    addItemType(JUDGE, R.layout.activity_test_question_item_judge);
    addItemType(FILL, R.layout.activity_test_question_item_fill);

}

@Override
protected void convert(BaseViewHolder helper, QuestionsBean.DataBean.ExampaperQuestionidBean item) {
    switch (helper.getItemViewType()) {
        case CHOICE: {
            final int position = helper.getLayoutPosition();
            mSelectedPositions.put(position, false);

            helper.setText(R.id.title_question_item1, item.getQuestions_title());

            if (item.getQuestions_img() != null) {
                GlideUtils.loadImage(mContext, LoadHtml.zhuangyi(IMG_URL + item.getQuestions_img()), (ImageView) helper.getView(R.id.img_exampaper_details_item1));
                helper.getView(R.id.img_exampaper_details_item1).setVisibility(View.VISIBLE);
            }

            RadioGroup mRadioGroup = helper.getView(R.id.mRadioGroup_exampaper_details_item1);
            RadioButton mRadioButton;
            RadioGroup.LayoutParams mButtonLayoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            courseid = item.getQuestions_id(); //id
            courseidList.add(position, courseid);
            mList.add(position, answer); //赋空值 为了数组不越界
            if (!item.getQuestions_option().get(0).equals("")) {

                for (int i = 0; i < item.getQuestions_option().size(); i++) {
                    mRadioButton = new RadioButton(mContext);
                    mRadioButton.setId(i);
                    mButtonLayoutParams.setMargins(0, 32, 0, 0);
                    mRadioButton.setText(item.getQuestions_option().get(i));
                    mRadioGroup.addView(mRadioButton, mButtonLayoutParams);
                }
                mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        selected(checkedId);
                        LogUtils.d("第", position + "个" + checkedId + "点击~");
                        LogUtils.d("第", selected);
                        answer = left + courseidList.get(position) + left + ":" + left + selected + left + ",";

                        //如果第n个被选中 mList就替换指定位置参数
                        if (mSelectedPositions.get(position)) {
                            LogUtils.d("第", "未选中");
                            mList.set(position, answer);
                        } else {
                            LogUtils.d("第", "选中");
                            mSelectedPositions.put(position, true);
                            mList.add(position, answer);
                        }
                        LogUtils.d("第", "!S" + mList.get(position));
                    }

                });
            }
            break;

        }
        case JUDGE: {
            final int position = helper.getLayoutPosition();
            mSelectedPositions.put(position, false);
            helper.setText(R.id.title_question_item2, item.getQuestions_title());
            RadioGroup mRadioGroup = helper.getView(R.id.mRadioGroup_exampaper_details_item2);
            RadioButton mRadioButton;
            RadioGroup.LayoutParams mButtonLayoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            courseid = item.getQuestions_id(); //id
            courseidList.add(position, courseid);
            mList.add(position, answer); //赋空值 为了数组不越界
            if (!item.getQuestions_option().get(0).equals("")) {
                for (int i = 0; i < item.getQuestions_option().size(); i++) {
                    mRadioButton = new RadioButton(mContext);
                    mRadioButton.setId(i);
                    mButtonLayoutParams.setMargins(0, 32, 0, 0);
                    mRadioButton.setText(item.getQuestions_option().get(i));
                    mRadioGroup.addView(mRadioButton, mButtonLayoutParams);
                }
                mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        mSelectedPositions.put(position, true);
                        selected(checkedId);
                        LogUtils.d("第", position + "个" + checkedId + "JUDGE");
                        LogUtils.d("第", selected + "JUDGE");
                        answer = left + courseidList.get(position) + left + ":" + left + selected + left + ",";

                        //如果第n个被选中 mList就替换指定位置参数
                        if (mSelectedPositions.get(position)) {
                            mList.set(position, answer);
                        } else {
                            mSelectedPositions.put(position, true);
                            mList.add(position, answer);
                        }
                    }
                });
            }
            break;
        }
        case FILL: {
            final int position = helper.getLayoutPosition();
            helper.setText(R.id.title_question_item3, item.getQuestions_title());
            EditText editText = helper.getView(R.id.editText_fill_item3);
            courseid = item.getQuestions_id(); //id
            courseidList.add(position, courseid);
            mList.add(position, answer); //赋空值 为了数组不越界
            mSelectedPositions.put(position, false);
            editText.addTextChangedListener(new TextWatcher() {

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    //文本变化中

                }

                @Override
                public void afterTextChanged(Editable s) {
                    //文本变化之后

                    selected = s + "";//内容
                    answer = left + courseidList.get(position) + left + ":" + left + selected + left + ",";
                    //如果第n个被选中 mList就替换指定位置参数
                    if (mSelectedPositions.get(position)) {
                        mList.set(position, answer);
                    } else {
                        mSelectedPositions.put(position, true);
                        mList.add(position, answer);
                    }
                }
            });


            break;
        }
        default: {
            break;
        }
    }

}

public String getAnswers() {
    String answe = "";
    for (int i = 0; i < mList.size(); i++) {
        answe = answe + mList.get(i);
    }
    answers = answe;
    return answers;
}

/*判断选中第几个赋值ABC...*/
private void selected(int checkedId) {
    switch (checkedId) {
        case 0: {
            selected = "A";
            break;
        }
        case 1: {
            selected = "B";
            break;
        }
        case 2: {
            selected = "C";
            break;
        }
        case 3: {
            selected = "D";
            break;
        }
        case 4: {
            selected = "E";
            break;
        }
        case 5: {
            selected = "F";
            break;
        }
        default: {
            break;
        }
    }
}

public SparseBooleanArray getmSelectedPositions() {
    return mSelectedPositions;
}

你可能感兴趣的:(Recyclerview 选择、判断题 的存储数值 是否为空 SparseBooleanArray的使用)