单击listview数据时如何在复选框上设置选中的值

时间:2014-03-11 08:08:03

标签: android android-listview

我的列表视图数据包含imageview,textview和复选框。当我点击listview数据时,一切正常。但复选框值不会改变。我想实现作为本机android多选项。我不仅要复制onClick事件复选框,还要复制imageview和textview。即用户点击一行的任何地方

这是我的代码。

public class ProductListChkAdapter extends BaseAdapter {

private Context mContext;
private static CashSaleProductInfo _CashSaleProductInfo = null;
private List<CashSaleProductInfo> _listChkCashSaleProductInfo;
private int chkPrdcodeID;
private LayoutInflater inflater;
public static List<CashSaleListInfo> _listCashSaleListInfo;
private CashSaleProductLogic _sellProductLogic;
boolean[] checkBoxState;

private LayoutInflater layoutInflater;

public ProductListChkAdapter(Context paramContext,
        List<CashSaleProductInfo> paramList1, int textViewResourceId) {
    this.mContext = paramContext;
    this._listChkCashSaleProductInfo = paramList1;
    this.chkPrdcodeID = textViewResourceId;

    checkBoxState = new boolean[_listChkCashSaleProductInfo.size()];
}

@Override
public int getCount() {
    return (_listChkCashSaleProductInfo == null) ? 0
            : _listChkCashSaleProductInfo.size();
}

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

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

@Override
public View getView(final int position, View convertView,
        ViewGroup paramViewGroup) {

    inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ViewHolder viewHolder = null;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.productinfochklist, null);

        TextView lblProductCode = (TextView) convertView
                .findViewById(R.id.lblProductCode);
        CheckBox chkPrdCode = (CheckBox) convertView
                .findViewById(R.id.chkProductCode);
        ImageView imgPrdCode = (ImageView) convertView
                .findViewById(R.id.main_list_item_pcode);
        RelativeLayout _relPrdlist = (RelativeLayout) convertView
                .findViewById(R.id.relPrdlist);

        imgPrdCode.setImageResource(R.drawable.c_f);

        viewHolder = new ViewHolder();
        viewHolder.lblProductCode = lblProductCode;
        viewHolder.chkPrdCode = chkPrdCode;
        viewHolder._relPrdlist = _relPrdlist;

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.lblProductCode.setText(_listChkCashSaleProductInfo
            .get(position).getDESC_ENG().toString());
viewHolder.chkPrdCode.setChecked(checkBoxState[position]);

    viewHolder._relPrdlist.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            MainActivity._CashSaleProductInfo = new CashSaleProductInfo();

            // CheckBox cb = (CheckBox) v;
            CheckBox cb = (CheckBox) v;

            int p = position;
            boolean b = checkBoxState[p];
            // if (((CheckBox) v).isChecked()) {
            if (!checkBoxState[position]) {
                // if (cb.isChecked()) {

                checkBoxState[position] = true;
                // States _state = (States) cb.getTag();

                MainActivity._CashSaleProductInfo
                        .setPRODUCT_CODE(_listChkCashSaleProductInfo.get(
                                position).getPRODUCT_CODE());
                MainActivity._CashSaleProductInfo
                        .setDESC_ENG(_listChkCashSaleProductInfo.get(
                                position).getDESC_ENG());
                MainActivity._listchkCashSaleProductInfo
                        .add(MainActivity._CashSaleProductInfo);
            } else {

                MainActivity._CashSaleProductInfo
                        .setPRODUCT_CODE(_listChkCashSaleProductInfo.get(
                                position).getPRODUCT_CODE());
                MainActivity._CashSaleProductInfo
                        .setDESC_ENG(_listChkCashSaleProductInfo.get(
                                position).getDESC_ENG());

                Iterator<CashSaleProductInfo> iterator = MainActivity._listchkCashSaleProductInfo
                        .iterator();
                while (iterator.hasNext()) {
                    CashSaleProductInfo _CashSaleProductInfo = iterator
                            .next();
                    if (_CashSaleProductInfo.getPRODUCT_CODE().equals(
                            MainActivity._CashSaleProductInfo
                                    .getPRODUCT_CODE())) {
                        iterator.remove();
                    } else {}
                }

                checkBoxState[position] = false;
                Log.i("CC", " After "
                        + MainActivity._listchkCashSaleProductInfo.size());
            }
        }
    });

    return convertView;
}

public static class ViewHolder {
    TextView lblProductCode;
    CheckBox chkPrdCode;
    ImageView imgPrdCode;
    RelativeLayout _relPrdlist;
}

}

2 个答案:

答案 0 :(得分:0)

尝试在onClick中添加此代码到您的复选框:

cb.setChecked(true);

cb.setChecked(false);

答案 1 :(得分:0)

尝试添加到Check Box setOnCheckedChangeListener 您可以在哪里存储CheckBox的状态

此处也链接Try this