重复选择android listview中的复选框

时间:2016-10-03 03:43:07

标签: android

使用listview中的复选框,我也能够获取所选值,但我的问题是选择第一个位置复选框时,自动选择第7个复选框。会有什么问题,请建议我如何解决这个问题。

公共类TransactionAdapter扩展了BaseAdapter {

    LayoutInflater inflator;
    String veh_reg_no;
    String fuel_qty, total_amt;

    public TransactionAdapter(Context context) {
        inflator = getLayoutInflater();
    }

    @Override
    public int getCount() {

        if (pso_transaction_list.size() != 0)
            return pso_transaction_list.size();
        return 0;

    }

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

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

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


        final Viewholder holder;
        if(convertView==null) {
            convertView = inflator.inflate(R.layout.pso_transation_layout, parent, false);

            holder=new Viewholder();

            holder.vT_tl_regnumber = (TextView) convertView.findViewById(R.id.vT_psotl_regnumber);
            holder.vT_tl_drivername = (TextView) convertView.findViewById(R.id.vT_psotl_drivername);
            holder.vT_tl_quantity = (TextView) convertView.findViewById(R.id.vT_psotl_quantity);
            holder.vT_tl_totalCost = (TextView) convertView.findViewById(R.id.vT_psotl_totalCost);
            holder.vT_psotl_payment = (TextView) convertView.findViewById(R.id.vT_psotl_payment);
            holder.vI_psotl_statusimg = (ImageView) convertView.findViewById(R.id.vI_psotl_statusimg);
            holder.vI_pha_Next = (ImageView) convertView.findViewById(R.id.vI_pha_Next);
            holder.vI_pha_select = (CheckBox) convertView.findViewById(R.id.vI_pha_select);
            holder.vT_psotl_unit = (TextView) convertView.findViewById(R.id.vT_psotl_unit);
            holder.vT_pha_rsr = (TextView) convertView.findViewById(R.id.vT_pha_rsr);
            holder.vL_psotl_nextlayout = (LinearLayout) convertView.findViewById(R.id.vL_psotl_nextlayout);
            holder.vL_psotl_checkboxlayout = (LinearLayout) convertView.findViewById(R.id.vL_psotl_checkboxlayout);
            convertView.setTag(holder);
        }else{
            holder = (Viewholder) convertView.getTag();
            holder.vI_pha_select.setOnCheckedChangeListener(null);

        }

        if (pso_transaction_list.get(position).getPaymentMode().getId() == 1 && pso_transaction_list.get(position).getWFStatus() == 7) {
            String pmt_status = "C/A | RCVD ";
            holder.vT_psotl_payment.setText(pmt_status);
        } else if (pso_transaction_list.get(position).getPaymentMode().getId() == 1) {
            holder.vT_psotl_payment.setText("C/A");
        } else {
            holder.vT_psotl_payment.setText("P/G");
        }

        if (pso_transaction_list.get(position).getFuelStations().getCountry() != null) {
            String[] currency = Getlocationaddress_by_address.currency(pso_transaction_list.get(position).getFuelStations().getCountry(), PsomytransactionlistActivity.this);
            holder.vT_pha_rsr.setText(" " + currency[0] + " ");
            holder.vT_psotl_unit.setText(" " + currency[1]);
        }




        holder.vI_pha_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked{

                if (isChecked) {

                    selected_list_payment.add(pso_transaction_list.get(position).getPaymentMode().getId() + "");
                    selected_list.add(pso_transaction_list.get(position).getId() + "");
                    selecteddetails.add(pso_transaction_list.get(position).getVehicleNo());
                    payment_status.add(pso_transaction_list.get(position).getWFStatus() + "");

                } else {

                    selected_list.remove(pso_transaction_list.get(position).getId() + "");
                    selected_list_payment.remove(pso_transaction_list.get(position).getPaymentMode().getId() + "");
                    selecteddetails.remove(pso_transaction_list.get(position).getVehicleNo());
                    payment_status.remove(pso_transaction_list.get(position).getWFStatus() + "");
                }
            }
        });


        veh_reg_no = pso_transaction_list.get(position).getVehicleNo();
        holder.vT_tl_regnumber.setText(veh_reg_no);
        holder.vT_tl_drivername.setText(pso_transaction_list.get(position).getDriver().getName());
        fuel_qty = " " + pso_transaction_list.get(position).getQuantity() + "";
        holder.vT_tl_quantity.setText(fuel_qty);
        total_amt = pso_transaction_list.get(position).getTotalPrice() + "";
        holder.vT_tl_totalCost.setText(total_amt);

        holder.vL_psotl_nextlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent trans_intent = new Intent(PsomytransactionlistActivity.this, Psotransactiondetails.class);
                trans_intent.putExtra("trans_details_position", position);
                startActivity(trans_intent);
            }
        });
        return convertView;
    }
}


public static class Viewholder{

    public TextView vT_tl_regnumber, vT_tl_drivername, vT_tl_quantity, vT_tl_totalCost, vT_psotl_payment, vT_psotl_unit, vT_pha_rsr;
    public ImageView vI_psotl_statusimg, vI_pha_Next;
    public LinearLayout vL_psotl_nextlayout, vL_psotl_checkboxlayout;
    public CheckBox vI_pha_select;
}

2 个答案:

答案 0 :(得分:1)

试试这种方式。这对我有用

public class CustomAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private final Context context;
    private List<ModelPooja> listData;

    public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
            holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
            holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checks.setOnCheckedChangeListener(null);
        holder.checks.setFocusable(false);

        if (listData.get(position).isselected) {
            holder.checks.setChecked(true);
        } else {
            holder.checks.setChecked(false);
        }

        holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton cb, boolean b) {

                if (checkMaxLimit()) {

                    if (listData.get(position).isselected && b) {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;

                    } else {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;
                        Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (b) {
                        listData.get(position).isselected = true;
                    } else {
                        listData.get(position).isselected = false;
                    }
                }
            }
        });

        holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
        return convertView;
    }

    public boolean checkMaxLimit() {
        int countermax = 0;
        for(ModelPooja item : listData){
            if(item.isselected){
                countermax++;
            }
        }
        return countermax >= 5;
    }

    public class ViewHolder {
        TextView tv;
        public CheckBox checks;
    }
}

对于Recyclerview试试这种方式

http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html

答案 1 :(得分:0)

    @Override
    public int getViewTypeCount() {
        return getCount();
    }
    @Override
    public int getItemViewType(int position) {
        return position;
    }

在你的“Adapter”类中添加两个函数 注意:如果使用 RecycleView,只需添加 getItemViewType(int position)。