如何从复选框数组中获取选定的复选框

Posted

技术标签:

【中文标题】如何从复选框数组中获取选定的复选框【英文标题】:How to get selected checkboxes from an array of checkboxes 【发布时间】:2021-10-07 15:52:15 【问题描述】:

我想保存选中的复选框并获取它们的字符串值。

我目前正在做的是使用一系列复选框(大约 20 cbs)

声明(onCreateView 之外)

CheckBox checkboxNslWing, checkboxNsfDoor, checkboxNsrDoor, checkboxNsrQuarter, checkboxNsRoofRail, checkboxOsfWing, checkboxOsfDoor, checkboxOsrDoor, checkboxOsrQuarter, checkboxOsRoofRail, checkboxBonnet, checkboxRoof, checkboxTailgate, checkboxNsSill, checkboxOsSill, checkboxNsSlidingDoor, checkboxOsSlidingDoor, checkboxFrontBumper, checkboxBackBumper, checkboxMotorbikeTank;

private CheckBox[] allCheckBoxes;

内部onCreateView

 

// Set Checkboxes properties
    
checkboxNslWing = (CheckBox) v.findViewById(R.id.NslWing);
checkboxNsfDoor = (CheckBox) v.findViewById(R.id.NsfDoor);
checkboxNsrDoor = (CheckBox) v.findViewById(R.id.NsrDoor);
checkboxNsrQuarter = (CheckBox) v.findViewById(R.id.NsrQuarter);
checkboxNsRoofRail = (CheckBox) v.findViewById(R.id.NsRoofRail);
checkboxOsfWing = (CheckBox) v.findViewById(R.id.OsfWing);
checkboxOsfDoor = (CheckBox) v.findViewById(R.id.OsfDoor);
checkboxOsrDoor = (CheckBox) v.findViewById(R.id.OsrDoor);
checkboxOsrQuarter = (CheckBox) v.findViewById(R.id.OsrQuarter);
checkboxOsRoofRail = (CheckBox) v.findViewById(R.id.OsRoofRail);
checkboxBonnet = (CheckBox) v.findViewById(R.id.Bonnet);
checkboxRoof = (CheckBox) v.findViewById(R.id.Roof);
checkboxTailgate = (CheckBox) v.findViewById(R.id.TailgateBoot);
checkboxNsSill = (CheckBox) v.findViewById(R.id.NsSill);
checkboxOsSill = (CheckBox) v.findViewById(R.id.OsSill);
checkboxNsSlidingDoor = (CheckBox) v.findViewById(R.id.NsSlidingDoor);
checkboxOsSlidingDoor = (CheckBox) v.findViewById(R.id.OsSlidingDoor);
checkboxFrontBumper = (CheckBox) v.findViewById(R.id.FrontBumper);
checkboxBackBumper = (CheckBox) v.findViewById(R.id.BackBumper);
checkboxMotorbikeTank = (CheckBox) v.findViewById(R.id.MotorbikeTank);

allCheckBoxes  = new CheckBox[] checkboxNslWing, checkboxNsfDoor, checkboxNsrDoor, checkboxNsrQuarter, checkboxNsRoofRail, checkboxOsfWing, checkboxOsfDoor, checkboxOsrDoor, checkboxOsrQuarter, checkboxOsRoofRail, checkboxBonnet, checkboxRoof, checkboxTailgate, checkboxNsSill, checkboxOsSill, checkboxNsSlidingDoor, checkboxOsSlidingDoor, checkboxFrontBumper, checkboxBackBumper, checkboxMotorbikeTank;

        int cb = 0;
        CheckListener listener = new CheckListener();
        for (CheckBox box : allCheckBoxes) 
            box.setOnCheckedChangeListener(listener);
            box.setTag(1 << cb);
            cb++;

            if (box.isChecked())
            
                Toast.makeText(getContext(), box.getText().toString(), Toast.LENGTH_LONG).show();
                Log.d("Operation", box.getText().toString());
            
        

onCheckedChanged


    class CheckListener implements CompoundButton.OnCheckedChangeListener 

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

            for (int i=0; i<=19; i++)
            
                if (allCheckBoxes[i].isChecked())
                
//                    Toast.makeText(getContext(), allCheckBoxes[i].getText().toString(), Toast.LENGTH_LONG).show();
                    Log.d("Operation", allCheckBoxes[i].getText().toString());
                
            
        
    

以上工作完全正常,我可以看到显示复选框被选中的日志,当我尝试将复选框保存在数据库中时,我的问题如下。虽然它有效,但它只保存了最后一个选中的复选框,而不是之前选中的任何其他复选框。例如。如果我检查 1,2,3,4 它只会保存数字 4。

else if ("Multiple".contentEquals(sel)) 
       
for (int i=0; i<=19; i++)

    if (allCheckBoxes[i].isChecked())
    
      damage.setDescription(sel+" "+allCheckBoxes[i].getText().toString());
    



保存到数据库的完整方法

    public String saveDamage(String vehicleId, String damageId) 
        Damage damage = new Damage();
        Vehicle vehicle = new Vehicle();
        damage.setVehicleId(new Integer(vehicleId));
        if (damageId != null) 
            damage.setId(new Integer(damageId));
        
        if (vehicleId != null) 
            vehicle.setId(new Integer(vehicleId));
        

        String sel = spinner.getSelectedItem().toString();
        if ("Left".contentEquals(sel) || "Right".contentEquals(sel)) 

            String sel3 = spinner3.getSelectedItem().toString();
            damage.setDescription(sel+" "+sel3);
        
        else if ("Front".contentEquals(sel)) 

            String sel2 = spinner2.getSelectedItem().toString();
            damage.setDescription(sel+" "+sel2);
        
        else if ("Rear".contentEquals(sel)) 

            String sel4 = spinner4.getSelectedItem().toString();
            damage.setDescription(sel+" "+sel4);
        
        else if ("Roof".contentEquals(sel)) 

            String sel5 = spinner5.getSelectedItem().toString();
            damage.setDescription(sel+" "+sel5);
        
        else if ("Multiple".contentEquals(sel)) 

            for (int i=0; i<=19; i++)
            
                if (allCheckBoxes[i].isChecked())
                
                    damage.setDescription(sel+" "+allCheckBoxes[i].getText().toString());
                
            

        

        damage.setNotes(txtEditNotes.getText().toString());

        try 
            damage.setCost(Double.valueOf(Objects.requireNonNull(txtEditCost.getText()).toString()));
         catch(NumberFormatException e) 
            damage.setCost(0d);
        

        String supCost = txtSupplementTotal.getText().toString().trim();
        supCost = supCost.replace("£", "");
        String supCostTotal = (String) (supCost);

        String deCost = txtDetailedEstimatorTotal.getText().toString().trim();
        deCost = deCost.replace("£", "");
        deCost = deCost.replace(",", "");
        String deCostTotal = (String) (deCost);

        damage.setSupplementTotal(Double.valueOf(supCostTotal));
        damage.setDetailedEstimatorTotal(Double.valueOf(deCostTotal));

        vehicle.setTotal(damage.getTotal());

        DamageRepository damageRepository = new DamageRepository(getActivity().getApplication());

        if (damageId != null) 
            damageRepository.update(damage);
            return damageId;
        

        long insertId = 0;
        try 
            Future<Long> future = damageRepository.insert(damage);
            insertId = future.get();
         catch (ExecutionException e) 
            e.printStackTrace();
            Toast.makeText(getContext(), "Failed saving data", Toast.LENGTH_LONG).show();
         catch (InterruptedException e) 
            e.printStackTrace();
            Toast.makeText(getContext(), "Failed saving data", Toast.LENGTH_LONG).show();
        
        return String.valueOf(insertId);
    

【问题讨论】:

看看你的循环:你不断覆盖上一次迭代的数据集,例如在这里:damage.setDescription(sel+" "+allCheckBoxes[i].getText().toString());。你认为damage.setDescription("sel 1"); damage.setDescription("sel 2"); 会导致什么结果?我假设您想追加 id,因此请相应地构建您的代码。 谢谢@Thomas 每个 sel 从使用的每个微调器中检索字符串值,我已经更改了它的表示方式,但问题不存在。 问题肯定存在。想想应该如何映射多个复选框和 one damage 对象。您需要拥有多个 damage 对象或组合您拥有的单个 damage 复选框的 ID。 【参考方案1】:

显然比我想象的要容易。

我在循环的每次迭代中都覆盖了损坏描述,即只保存了最后一个。

解决方案

    else if ("Multiple".contentEquals(sel)) 
                String d = sel;
                for (int i=0; i<=19; i++)
                
                    if (allCheckBoxes[i].isChecked())
                    
                        String sel5 = allCheckBoxes[i].getText().toString();
                        d += (" "+sel5);
                    
                
                damage.setDescription(d);
            

【讨论】:

以上是关于如何从复选框数组中获取选定的复选框的主要内容,如果未能解决你的问题,请参考以下文章

如何从黑莓10级联中的复选框选定项目中获取值..?

获取选定复选框属性的数组[重复]

如何使用复选框和自定义适配器从 Listview 中获取选定的列表项?

从Swift数组中的选定复选标记中删除字符串

如何在 WPF CheckBox ListBox 中获取选定项目

从复选框形式制作数组