这有可能在android中获取listview项目的子位置吗?

时间:2015-11-05 05:07:26

标签: android listview

我有......

  
      
  1. 问题1 - >列表视图项目a)复选框1 b)复选框2 c)复选框3 d)复选框4
  2.   
  3. 问题2 - >列表视图项目a)复选框1 b)复选框2 c)复选框3 d)复选框4 e)复选框5 f)复选框6
      g)复选框7 h)复选框8 i)复选框9
  4.   
  5. 问题3 - >列表视图项目a)单选按钮1 b)单选按钮2 c)单选按钮3
  6.   
  7. 问题4 - >列表视图项目a)复选框1 b)复选框2
  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;
                    }

我想获得单选按钮和复选框的位置。因为它们在List中,所以当我滚动它们时会自动取消选中。我知道getView()具有转换视图的属性,但我想做这个...我是这个概念的新手,请帮我找到解决方案...

提前致谢...

1 个答案:

答案 0 :(得分:1)

您也可以自己管理视图。为您的商品创建每个独特的视图,不要回收视图。这样可以防止您的复选框被取消选中

private Map<Integer, View> myViews = new HashMap<Integer, View>(); 

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View view = myViews.get(position);
    if (view == null) {
        view = myInflater.inflate(R.layout.list_add_friends_row, null);
        //don't need use the holder anymore.

      //Your code
       myViews.put(position, view);

    }
    return view;
}