samedi 11 septembre 2021

How to set multiple checkboxes checked based on a string array

What I am trying to do is get the string values which are saved in the database and if these values equal to the cheboxes string values then check those checkboxes.

Without going into a lot of detail, I am doing the folowing inside observe(getViewLifecycleOwner(), d -> { in my fragment

String desc = d.damage.getDescription().trim();
Log.d("checkbox Op #1",desc);

Which will return something like this (depends on how many are saved at a time)

D/checkbox Op #1: NSR door NSR quarter NS roof rail OSR door OSR quarter NS sill OS sill NS sliding door OS sliding door

I do the following to get the checkboxes string values and compare them


for (int i=0; i<=19; i++)
{

    String sel5 = allCheckBoxes[i].getText().toString().trim();
    Log.d("checkbox Op #2", sel5);

    if (allCheckBoxes[i].getText().toString().trim().equals(desc))
    {
        allCheckBoxes[i].setChecked(true);
        Log.d("checkbox Op #3", allCheckBoxes[i].getText().toString());
    }
}

which the above will return

D/checkbox Op #2: NSL wing
    NSF door
    NSR door
    NSR quarter
    NS roof rail
    OSF wing
    OSF door
    OSR door
    OSR quarter
    OS roof rail
    Bonnet
    Roof
    Tailgate/boot
    NS sill
    OS sill
    NS sliding door
    OS sliding door
    Front bumper
    Back bumper
    Motorbike tank

The problem: The above will work fine if I am retring 1 value from the database when using String desc = d.damage.getDescription().trim(); however in most cases it will be more then 1 as explained earlier which in that case the code wont work and I am not sure what I am missing.

full code


String desc = d.damage.getDescription().trim();

Log.d("checkbox Op #1",desc);

for (int i=0; i<=19; i++)
{

    String sel5 = allCheckBoxes[i].getText().toString().trim();
    Log.d("checkbox Op #2", sel5);

    if (allCheckBoxes[i].getText().toString().trim().equals(desc))
    {
        allCheckBoxes[i].setChecked(true);
        Log.d("checkbox Op #3", allCheckBoxes[i].getText().toString());
    }
}




Aucun commentaire:

Enregistrer un commentaire