自动检查ListView问题中的复选框以及最后一个位置

时间:2016-02-16 08:31:35

标签: android listview button checkbox

我正在CheckBox中实施ListView。我遇到的问题是,只要我们检查另一个CheckBox,就会自动检查CheckBox的最后一个位置。

以下是我实施的adapter

 private class ListAdapter_cb extends BaseAdapter {
    private LayoutInflater inflater;
    private ViewHolder vh;
    CheckboxesPOJO cc = new CheckboxesPOJO();
    List<CheckboxesPOJO> array = new ArrayList<CheckboxesPOJO>();
    boolean[] checkedState;
    private Context context;
    boolean[] checkboolean;
    int pos;
    List<String> answers;
    public ListAdapter_cb(Context c, int early_pos, List<CheckboxesPOJO> arraylist) {
        this.array = arraylist;
        this.context = c;
        this.pos = early_pos;
        checkboolean = new boolean[array.size()];
    }
    @Override
    public View getView(final int position, View convertview, ViewGroup parent) {
        final CheckboxesPOJO obj_bean_downloadplus = array.get(position);
        if (convertview == null) {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertview = inflater.inflate(R.layout.element_cb, parent, false);
            vh = new ViewHolder();
            vh.answer_checkbox = (CheckBox) convertview.findViewById(R.id.answer_checkbox);
            convertview.setTag(vh);
        } else {
            vh = (ViewHolder) convertview.getTag();
        }

        vh.answer_checkbox.setText(obj_bean_downloadplus.getItsanswer() + "");
        vh.answer_checkbox.setId(position);
        vh.answer_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                {
                    if ((buttonView.isChecked())) {
                        vh.answer_checkbox.setChecked(true);
                        checkboolean[position] = true;
                        checkedList.add(obj_bean_downloadplus.getNumber() + ":" + obj_bean_downloadplus.getItsanswer());
                        Log.v("", TAG + "obj_bean_downloadplus >> " + obj_bean_downloadplus.getNumber() + ":" + obj_bean_downloadplus.getItsanswer());
                        Log.v("", TAG + "obj_bean_downloadplus >> " + checkedList.toString());

                    } else {
                        vh.answer_checkbox.setChecked(false);
                        checkboolean[position] = false;
                        checkedList.remove(obj_bean_downloadplus.getNumber() + ":" + obj_bean_downloadplus.getItsanswer());
                        Log.v("", TAG + "obj_bean_downloadplus >> " + obj_bean_downloadplus.getNumber() + ":" + obj_bean_downloadplus.getItsanswer());
                        Log.v("", TAG + "obj_bean_downloadplus >> " +  checkedList.toString());

                    }
                }
            }
        });

        vh.answer_checkbox.setChecked(checkboolean[position]);

        return convertview;
    }

    public int getCount() {
        return array.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}

这导致checkbox的最后一个位置自动检查,甚至将其答案保存在checkedlist中,我只需要发送那些被检查的位置。

/// ==单选按钮===== // 同样的问题我在电台组面临。 请在下面找到radiogroup adater:

  private class RadioGroupAdapter extends BaseAdapter {
    List<SubquesPOJO> arraylist_downloadplus = new ArrayList<SubquesPOJO>();
    Context context;
    int layoutResourceId;
    int early_pos;
    List<String> arraylist_downloadplusanswer = new ArrayList<String>();
    String[] ansarray;
    private SparseBooleanArray mSparseBooleanArray_radio;

    public RadioGroupAdapter(Context context, int early_pos, int layoutResourceId, List<SubquesPOJO> arraylist_downloadplus1, List<String> arraylist_downloadplus2) {
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.early_pos = early_pos;
        arraylist_downloadplus.clear();
        arraylist_downloadplusanswer.clear();
        arraylist_downloadplus = arraylist_downloadplus1;
        arraylist_downloadplusanswer = arraylist_downloadplus2;
        ansarray = new String[arraylist_downloadplus.size()];
        mSparseBooleanArray_radio = new SparseBooleanArray();
        Log.v("", TAG + "=arraylist_downloadplus.size()=" + arraylist_downloadplus.size());
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final SubquesPOJO obj_bean_downloadplus = arraylist_downloadplus.get(position);
        View row = convertView;
        MatrixHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new MatrixHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.txt_misssion);
            holder.group = (RadioGroup) row.findViewById(R.id.answer_radio_group);
            holder.rb2 = new RadioButton[arraylist_downloadplusanswer.size()];
            for (int j = 0; j < arraylist_downloadplusanswer.size(); j++) {
                holder.rb2[j] = new RadioButton(context);
                RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(0, 0, 20, 0);
                holder.group.addView(holder.rb2[j], params);
                Log.v("", "=ansarray[position]=" + ansarray[position]);
            }
            row.setTag(holder);
        } else {
            holder = (MatrixHolder) row.getTag();
        }
        holder.txtTitle.setText(obj_bean_downloadplus.getTitle());
        for (int j = 0; j < holder.rb2.length; j++) {
            holder.rb2[j].setText(j + 1 + "");
            holder.rb2[j].setId(j);
            holder.rb2[j].setTag(j + 1);
            holder.rb2[j].setTextColor(getResources().getColor(R.color.text_grey_dark));
            //  holder.rb2[j].setOnCheckedChangeListener(new CheckListner_radio(obj_bean_downloadplus, holder.rb2[j]));
           /* if (obj_bean_downloadplus.isSelected()) {
                holder.rb2[j].setChecked(true);
            } else {
                holder.rb2[j].setChecked(false);
            }*/
            holder.rb2[j].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String ansId = v.getTag().toString();
                    ansarray[position] = "\"" + (position + 1) + ":" + ansId + "\"";
                    Log.v("", "=position:ansId=" + ansarray[position]);
                }
            });
            //  mSparseBooleanArray_radio.put((Integer) holder.rb2[j].getTag(), holder.rb2[j].isChecked());
        }
        return row;
    }

    class CheckListner_radio implements CompoundButton.OnCheckedChangeListener {
        private RadioButton checkbox;
        SubquesPOJO data;

        public CheckListner_radio(SubquesPOJO data, RadioButton checkbox) {
            this.data = data;
            this.checkbox = checkbox;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                checkbox.setChecked(true);
                String ansId = buttonView.getTag().toString();
                ansarray[position] = "\"" + (position + 1) + ":" + ansId + "\"";
                Log.v("", "=position:ansId=" + ansarray[position]);
                data.SetIsSelected(true);
            }/* else {
                checkbox.setChecked(false);
                data.SetIsSelected(false);
            }*/
            mSparseBooleanArray_radio.put((Integer) buttonView.getTag(), isChecked);
        }
    }

    @Override
    public int getCount() {
        return arraylist_downloadplus.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}

有些行被注释,我已经实现了相同的复选框,但它没有用。

请帮忙。

2 个答案:

答案 0 :(得分:1)

private class ListAdapter_cb extends BaseAdapter {
    private LayoutInflater inflater;
    private ViewHolder vh;
    CheckboxesPOJO cc = new CheckboxesPOJO();
    List<CheckboxesPOJO> array = new ArrayList<CheckboxesPOJO>();
    boolean[] checkedState;
    private Context context;
    boolean[] checkboolean;
    private SparseBooleanArray mSparseBooleanArray;
    int pos;
    List<String> answers;
    public ListAdapter_cb(Context c, int early_pos, List<CheckboxesPOJO> arraylist) {
        this.array = arraylist;
        this.context = c;
        this.pos = early_pos;
        mSparseBooleanArray = new SparseBooleanArray();
        checkboolean = new boolean[array.size()];
    }
    @Override
    public View getView(final int position, View convertview, ViewGroup parent) {
        final CheckboxesPOJO obj_bean_downloadplus = array.get(position);
        if (convertview == null) {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertview = inflater.inflate(R.layout.element_cb, parent, false);
            vh = new ViewHolder();
            vh.answer_checkbox = (CheckBox) convertview.findViewById(R.id.answer_checkbox);
            convertview.setTag(vh);
        } else {
            vh = (ViewHolder) convertview.getTag();
        }

        vh.answer_checkbox.setText(obj_bean_downloadplus.getItsanswer() + "");
        vh.answer_checkbox.setId(position);
        vh.answer_checkbox.setOnCheckedChangeListener(new CheckListener());
vh.answer_checkbox.setTag(position);
        vh.answer_checkbox.setChecked(checkboolean[position]);

        return convertview;
    }

    public ArrayList<CheckboxesPOJO> getCheckedItems() {
        ArrayList<CheckboxesPOJO> mTempArry = new ArrayList<CheckboxesPOJO>();

        for (int i = 0; i < mList.size(); i++) {
            if (mSparseBooleanArray.get(i)) {

                CheckboxesPOJO data = mList.get(i);

                mTempArry.add(data);
            }
        }

        return mTempArry;
    }
    class CheckListener implements OnCheckedChangeListener {

        private CheckBox checkbox;
CheckboxesPOJO data;
        public CheckListener(CheckboxesPOJO data, CheckBox checkbox) {
this.data = data;
            this.checkbox = checkbox;

        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if (isChecked) {
                        checkbox.setChecked(true);
                        checkedList.add(data.getNumber() + ":" + data.getItsanswer());

                        Log.v("", TAG + "data >> " + data.getNumber() + ":" + data.getItsanswer());
                        Log.v("", TAG + "data >> " + checkedList.toString());

                    } else {
                        checkbox.setChecked(false);

                        checkedList.remove(data.getNumber() + ":" + data.getItsanswer());
                        Log.v("", TAG + "data >> " + data.getNumber() + ":" + data.getItsanswer());
                        Log.v("", TAG + "data >> " +  checkedList.toString());

                    }

            mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
        }

    }

    public int getCount() {
        return array.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}

答案 1 :(得分:1)

In your Pojo class

CheckboxesPOJO.class

add a new parameter

boolean isSelected;

void SetIsSelected(boolean isSelected)
{
this.isSelected = isSelected;
}
boolean isSelected()
{
return isSelected;
}

and in getView Method you need to implement check for this

 public View getView(final int position, View convertview, ViewGroup parent) {
        final CheckboxesPOJO obj_bean_downloadplus = array.get(position);
        if (convertview == null) {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertview = inflater.inflate(R.layout.element_cb, parent, false);
            vh = new ViewHolder();
            vh.answer_checkbox = (CheckBox) convertview.findViewById(R.id.answer_checkbox);
            convertview.setTag(vh);
        } else {
            vh = (ViewHolder) convertview.getTag();
        }

        vh.answer_checkbox.setText(obj_bean_downloadplus.getItsanswer() + "");
        vh.answer_checkbox.setId(position);
        vh.answer_checkbox.setOnCheckedChangeListener(new CheckListener());
vh.answer_checkbox.setTag(position);
if(obj_bean_downloadplus.isSelcetd())
{
        vh.answer_checkbox.setChecked(true);
}
else
{
 vh.answer_checkbox.setChecked(false);
}
        return convertview;
    }

class CheckListener implements OnCheckedChangeListener {

        private CheckBox checkbox;
CheckboxesPOJO data;
        public CheckListener(CheckboxesPOJO data, CheckBox checkbox) {
this.data = data;
            this.checkbox = checkbox;

        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if (isChecked) {
                        checkbox.setChecked(true);
data.setIsSelected(true);
                        checkedList.add(data.getNumber() + ":" + data.getItsanswer());

                        Log.v("", TAG + "data >> " + data.getNumber() + ":" + data.getItsanswer());
                        Log.v("", TAG + "data >> " + checkedList.toString());

                    } else {
data.setIsSelected(false);
                        checkbox.setChecked(false);

                        checkedList.remove(data.getNumber() + ":" + data.getItsanswer());
                        Log.v("", TAG + "data >> " + data.getNumber() + ":" + data.getItsanswer());
                        Log.v("", TAG + "data >> " +  checkedList.toString());

                    }

            mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
        }

    }