I would like to save checked checkboxes and get their string value.
What I am currently doing is using an array of checkboxes (about 20 cbs)
Declarations (outside 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;
Inside 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());
}
}
}
}
The above works completely fine and I can see the logs which show the checkboxes being checked, my issue is below when trying to save the checkboxes in the database. Though it works it only saved the last checked checkbox and not any other checkboxes previously checked. E.g. if I check 1,2,3,4 it will only save the number 4.
else if ("Multiple".contentEquals(sel)) {
for (int i=0; i<=19; i++)
{
if (allCheckBoxes[i].isChecked())
{
damage.setDescription(sel+" "+allCheckBoxes[i].getText().toString());
}
}
}
Full method for saving into db
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);
}
Aucun commentaire:
Enregistrer un commentaire