如何获取动态添加的复选框和单选按钮的位置作为Listview项的子位置



如何进行

  1. getView()中动态添加一个CheckBox
  2. 为具有不同计数的CheckBox设置文本。。。

    在解释中。。。

在Listview 中

  • 问题1需要5个复选框
  • 问题2需要7个复选框
  • 问题3需要3个复选框
  • 问题4需要8个复选框

这就是我想要的,我使用了代码。。。

@SuppressWarnings("deprecation")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
            view = mLayoutInflater.inflate(R.layout.question_items, null);
            final TextView question_number = (TextView) view.findViewById(R.id.qno_text);
            final TextView question = (TextView) view.findViewById(R.id.qstn_text);
            LinearLayout linear_layout = (LinearLayout) view.findViewById(R.id.linear_vertical);
            question_number.setText(question_items.get(position).getQuestionNo());
            question.setText(question_items.get(position).getQuestion());
            String single_or_multiple = question_items.get(position).getSingleSelect();
    if(single_or_multiple.equals("0")) {
        final ArrayList<String> answer_check_box = new ArrayList<>();
        final ArrayList<String> questions = new ArrayList<>();
        final ArrayList<String> questions_in_ques_and_ans = new ArrayList<>();
        final ArrayList<String> questions_and_checking = new ArrayList<>();
        checkBoxState = new boolean[question_items.get(position).getOptionList().size()];
        final int check_size = question_items.get(position).getOptionList().size();
        Log.d("Size", String.valueOf(check_size));
        for(int i = 0; i<question_items.get(position).getOptionList().size(); i++) {
            check_box = new CheckBox(context);
            check_box.setGravity(Gravity.CENTER);
            check_box.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.fifth_username));
            check_box.setPadding(8, 8, 8, 8);
            check_box.setText(question_items.get(position).getOptionList().get(i).getOPTIONS());
            LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
            params.setMargins(10, 10, 10, 10);
            check_box.setLayoutParams(params);
            check_box.setId(i);
            check_box.setTag(i);
            check_box.setChecked(false);
            check_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int id = buttonView.getId();
                    Log.d("ID", String.valueOf(id));
                    tag_val = (int) buttonView.getTag();
                    String tag_value = String.valueOf(tag_val);
                    Log.d("TAG", tag_value);
                    Question_Number = question_number.getText().toString();
                    Question = question.getText().toString();
                    String value = "";
                            if(isChecked) {
                                value = question_items.get(position).getOptionList().get(id).getOPTIONS();
                                    Log.d("checkbox checked value", value);
                                answer_check_box.add(value);
                                if(questions.size() > 0) {
                                            if(questions.contains(Question)) {
                                                questions.remove(Question);
                                                questions.add(Question);
                                                } else {
                                                    questions.add(Question);
                                                }
                                        } else {
                                            questions.add(Question);
                                        }
                                Log.d("Answers Checked", answer_check_box.toString());
                                Log.d("Question Checked", questions.toString());
                                Answer = answer_check_box.toString();
                                    }else {
                                value = question_items.get(position).getOptionList().get(id).getOPTIONS();
                                Log.d("checkbox Unchecked value", value);
                                    answer_check_box.remove(value);
                                    if(answer_check_box.size() > 0) {
                                            } else {
                                            questions.remove(Question);
                                        }
                                Log.d("Answers Unchecked", answer_check_box.toString());
                                Log.d("Question UnChecked", questions.toString());
                                Answer = answer_check_box.toString();
                                      }
                            if(questions_and_checking.size() > 0) {
                                Log.d("Size","above 1");
                                for(int check = 0; check < questions_and_checking.size(); check++) {
                                    questions_in_ques_and_ans.add(questions_and_checking.get(check));
                                        }
                                Log.d("Questions check", questions_in_ques_and_ans.toString());
                                Log.d("Que array", ques_and_ans.toString());
                                if(questions_in_ques_and_ans.contains(Question)) {
                                    Log.d("Contains","Que");
                                    int index = ques_and_ans.indexOf(ques_and_ans.get(position));
                                    Log.d("Index", String.valueOf(index));
                                    ques_and_ans.remove(index);
                                    Question_and_Answer item = new Question_and_Answer();
                                    item.setQustionNumber(Question_Number);
                                    item.setQuestionTitle(Question);
                                    item.setOptionValue(Answer);
                                    ques_and_ans.add(item);
                                    questions_and_checking.add(Question);
                                    } else {
                                        Log.d("Not Contains","Que");
                                        Question_and_Answer item = new Question_and_Answer();
                                        item.setQustionNumber(Question_Number);
                                        item.setQuestionTitle(Question);
                                        item.setOptionValue(Answer);
                                        ques_and_ans.add(item);
                                        questions_and_checking.add(Question);
                                        }
                             } else {
                                 Log.d("Size", "0");
                                    Question_and_Answer item = new Question_and_Answer();
                                    item.setQustionNumber(Question_Number);
                                    item.setQuestionTitle(Question);
                                    item.setOptionValue(Answer);
                                    ques_and_ans.add(item);
                                    questions_and_checking.add(Question);
                                 Log.d("Check After First add", ques_and_ans.toString());
                               }
                            }
                        });
            linear_layout.addView(check_box);
            }

        } else {
                    radio_group = new RadioGroup(context);
                for(int i=0; i<question_items.get(position).getOptionList().size(); i++) {
                    radio_button = new RadioButton(context);
                    radio_button.setGravity(Gravity.CENTER);
                    radio_button.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.fifth_username));
                    radio_button.setPadding(8, 8, 8, 8);
                    radio_button.setText(question_items.get(position).getOptionList().get(i).getOPTIONS());
                    LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
                    params.setMargins(10, 10, 10, 10);
                    radio_button.setLayoutParams(params);
                    radio_group.addView(radio_button);
                            }
                linear_layout.addView(radio_group);
                radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup rgp, int checkedId) {
                        Question_Number =question_number.getText().toString();
                        Question = question.getText().toString();
                        radioButtonID = radio_group.getCheckedRadioButtonId();
                        View radioButton = radio_group.findViewById(radioButtonID);
                        idx = radio_group.indexOfChild(radioButton);
                        Log.d("id", String.valueOf(idx));
                        String answer = question_items.get(position).getOptionList().get(idx).getOPTIONS();
                        Toast.makeText(getApplicationContext(), answer, 2).show();
                        Answer = answer;
                        Question_and_Answer item = new Question_and_Answer();
                        item.setQustionNumber(Question_Number);
                        Log.d("Q_no", Question_Number);
                        item.setQuestionTitle(Question);
                        Log.d("Qstn", Question);
                        item.setOptionValue(Answer);
                        Log.d("Answer", Answer);
                        ques_and_ans.add(item);
                                }
                            });
                        }
                    return view;
                        }

我无法获取复选框和单选按钮的位置。。。因为当我滚动时,它会自动取消选中。我知道它是getView的属性,但我如何用这段代码来控制它。。。

是否为具有不同计数的CheckBoxes设置文本?

每个CheckBox都有一个setText()-函数,您可以用所需的文本调用它。

在getView中动态添加CheckBox

您的ViewHolder包含一个LinearLayout。假设你想用CheckBoxes填充它,你应该能够

CheckBox cb = new CheckBox(this);
cb.setText("some text");
linear_layout.addView(cb);

对于您想要添加的每个CheckBox。您可能还希望设置onCheckedChangeListener,以便在用户检查CheckBox时得到通知。

也许有一种比在getView()中添加这些元素更简单的方法?

但问题是转换视图不起作用。因为我的第三个和第四个问题分别有3个和8个选项。但它只显示了5选项。。。

假设复选框的数量在CCD_ 14中,你应该能够做

for ( int i = 0; i < question_items.get(position).getQuestionNo(); i++ ) {
    //code to add a single checkbox
}

如果你想遵循@Jawad关于设置可见性的方法,你也可以在循环中这样做。

相关内容

  • 没有找到相关文章

最新更新