5 input based search form for Android 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 != "" &amp;&amp; name != "" &amp;&amp; breed != "Breed" &amp;&amp; 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() &lt; 1){
        Toast.makeText(getContext(), "Sorry, No Cattle with those details!", Toast.LENGTH_LONG).show();
    }

} else if (tag == "" &amp;&amp; name == "" &amp;&amp; breed == "Breed"){
    cursor = cupboard().withDatabase(sqLiteDatabase).query(Cattle.class)
            .withSelection("sex LIKE ?", sex)
            .getCursor();
    if(cursor.getCount() &lt; 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() &lt; 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() &lt; 1){
        Toast.makeText(getContext(), "Sorry, No Cattle with those details!", Toast.LENGTH_LONG).show();
    }

} else if (tag == "" &amp;&amp; name == "" &amp;&amp; sex == "Sex"){
    cursor = cupboard().withDatabase(sqLiteDatabase).query(Cattle.class)
            .withSelection("breed LIKE ?", breed)
            .getCursor();
    if(cursor.getCount() &lt; 1){
        Toast.makeText(getContext(), "Sorry, No Cattle with those details!", Toast.LENGTH_LONG).show();
    }

}
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() &lt; 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&lt;String, String&gt;();

if(breed != "" &amp;&amp; breed != "Breed")
{
   selectionOptions.Add("breed LIKE ?", breed);
}

if(sex != "" &amp;&amp; sex != "Sex")
{
   selectionOptions.Add("sex LIKE ?", sex);
}

// same for tag and name 

// now construct the selection
var selection = string.Join(" AND ", selectionOptions.Keys);

// pass the selection and its arguments to the method I mentioned above
ThatMethodMentionedAbove(selection, selectionOptions.Values);


The logic being:


Look at each of the four possible parameters
Only add a parameter to the query if it has a value and its value is not its own name


That would make sense to me, whereas this kind of logic:

if (tag == "" &amp;&amp; name == "" &amp;&amp; breed == "Breed"){
    // snip
    .withSelection("sex LIKE ?", sex)


... just looks... odd. And indeed, very un-maintainable.