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 EditTexts in which the user can have alphanumeric values and 3 Spinners. 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();    }}
5 input based search form for Android
java;performance;algorithm;android;sqlite
Disclaimer: I'm a C# developer and I haven't written Java in ages.First of all I'd move this to a separate method, with (String selection, String[] selectionArgs) as parameters:cursor = cupboard().withDatabase(sqLiteDatabase).query(Cattle.class)        .withSelection(selection, selectionArgs)        .getCursor();if(cursor.getCount() < 1){    Toast.makeText(getContext(), Sorry, No Cattle with those details!, Toast.LENGTH_LONG).show();You can now call that method in each if instead of copy-pasting the same code over and over again and only changing a small part.But I'm mostly baffled by the convoluted logic WRT those ifs and the resulting selection and selectionArgs. Are you sure you didn't mean to construct something like this (pseudo-code):var selectionOptions = new Dictionary<String, String>();if(breed !=  && breed != Breed){   selectionOptions.Add(breed LIKE ?, breed);}if(sex !=  && sex != Sex){   selectionOptions.Add(sex LIKE ?, sex);}// same for tag and name // now construct the selectionvar selection = string.Join( AND , selectionOptions.Keys);// pass the selection and its arguments to the method I mentioned aboveThatMethodMentionedAbove(selection, selectionOptions.Values);The logic being:Look at each of the four possible parametersOnly add a parameter to the query if it has a value and its value is not its own nameThat would make sense to me, whereas this kind of logic:if (tag ==  && name ==  && breed == Breed){    // snip    .withSelection(sex LIKE ?, sex)... just looks... odd. And indeed, very un-maintainable.