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());
}
}
CodePudding user response:
The problem is that not of your checkboxes have a text that is equal to "NSR door NSR quarter NS roof rail OSR door OSR quarter NS sill OS sill NS sliding door OS sliding door".
There are two possible solutions.
The first is what I would prefer: don't return a single String
with all the possible values from the database, but fetch it as a List<String>
.
Then your code would look like:
List<String> descs = d.damage.getDescriptions();
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 (descs.contains(sel5))
{
allCheckBoxes[i].setChecked(true);
Log.d("checkbox Op #3", sel5);
}
}
The other (IMHO inferior) solution would be to check if the desc
string contains the checkbox value:
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 (desc.conatains(sel5))
{
allCheckBoxes[i].setChecked(true);
Log.d("checkbox Op #3", sel5);
}
}
This is dangerous if one of the checkbox values is a substring of one of the other values (for example, "NSF door"
and "SF door"
would be problematic).