I've been trying to look for a better way in which a user has up to 5 inputs in which they can search for results in a database.
I have 2 EditText
s in which the user can have alphanumeric values and 3 Spinner
s. As you can see in the code, it's badly designed in the way it consists of if
or else if
statements which is just horrible, but it works.
What would be the best way to avoid long if
or else if
statements and be able to pick and choose what you would like to search for in the database? I know there is the switch
but does that not just take a value and return the result of something on the one case
?
if(tag != "" && name != "" && breed != "Breed" && sex != "Sex"){
cursor = cupboard().withDatabase(sqLiteDatabase).query(Cattle.class)
.withSelection("tag LIKE ? AND name LIKE ? AND breed LIKE ? AND sex LIKE ?", tag, name, breed, sex)
.getCursor();
if(cursor.getCount() < 1){
Toast.makeText(getContext(), "Sorry, No Cattle with those details!", Toast.LENGTH_LONG).show();
}
} else if (tag == "" && name == "" && breed == "Breed"){
cursor = cupboard().withDatabase(sqLiteDatabase).query(Cattle.class)
.withSelection("sex LIKE ?", sex)
.getCursor();
if(cursor.getCount() < 1){
Toast.makeText(getContext(), "Sorry, No Cattle with those details!", Toast.LENGTH_LONG).show();
}
} else if (breed != "Breed"){
cursor = cupboard().withDatabase(sqLiteDatabase).query(Cattle.class)
.withSelection("breed LIKE ?", breed)
.getCursor();
if(cursor.getCount() < 1){
Toast.makeText(getContext(), "Sorry, No Cattle with those details!", Toast.LENGTH_LONG).show();
}
} else if (sex != "Sex"){
cursor = cupboard().withDatabase(sqLiteDatabase).query(Cattle.class)
.withSelection("sex LIKE ?", sex)
.getCursor();
if(cursor.getCount() < 1){
Toast.makeText(getContext(), "Sorry, No Cattle with those details!", Toast.LENGTH_LONG).show();
}
} else if (tag == "" && name == "" && sex == "Sex"){
cursor = cupboard().withDatabase(sqLiteDatabase).query(Cattle.class)
.withSelection("breed LIKE ?", breed)
.getCursor();
if(cursor.getCount() < 1){
Toast.makeText(getContext(), "Sorry, No Cattle with those details!", Toast.LENGTH_LONG).show();
}
}