Ahmed Raza
Ahmed Raza

Reputation: 73

Switch statement not displaying any results

I am using android Staggered view library by Etsy and I wanted to add data on each grid. So I used switch statement in place of for loop. But no results are displaying. Is there any syntax error or should I use any other statement here? I have commented the for loop that was originally there. Help me in being able to display strings.

package com.etsy.android.sample;

import java.util.ArrayList;

public class SampleData {

public static final int SAMPLE_DATA_ITEM_COUNT = 6;

public static ArrayList<String> generateSampleData() {
    final ArrayList<String> data = new ArrayList<String>(SAMPLE_DATA_ITEM_COUNT);

    switch (SAMPLE_DATA_ITEM_COUNT) {

        case 0:
            data.add("New Delhi");
            break;


        case 1:
            data.add("Mumbai");
            break;


        case 2:
            data.add("Kanpur");
            break;


        case 3:
            data.add("Hyderabad");
            break;


        case 4:
            data.add("Bangalore");
            break;


        case 5:
            data.add("Noida");
            break;


    }

    /*
    for (int i = 0; i < SAMPLE_DATA_ITEM_COUNT; i++) {
        data.add("SAMPLE #");
    }

    */

    return data;


}
}

Upvotes: 1

Views: 183

Answers (1)

HexAndBugs
HexAndBugs

Reputation: 5789

You can't replace a for loop with a switch statement. They are for totally different purposes. The for loop will run the line

data.add("SAMPLE #");

six times, but your switch statement will be executed only once and it will look for the item that matches 6, which in your case there isn't one, so that's why nothing is displayed. If you changed the value of SAMPLE_DATA_ITEM_COUNT to a number between 1 and 5 you'd see one item. To help you understand, your switch statement is exactly the same as the following if statement:

if (SAMPLE_DATA_ITEM_COUNT == 0) {
        data.add("New Delhi");
} else if (SAMPLE_DATA_ITEM_COUNT == 1) {
        data.add("Mumbai");
} else if (SAMPLE_DATA_ITEM_COUNT == 2) {
        data.add("Kanpur");
} else if (SAMPLE_DATA_ITEM_COUNT == 3) {
        data.add("Hyderabad");
} else if (SAMPLE_DATA_ITEM_COUNT == 4) {
        data.add("Bangalore");
} else if (SAMPLE_DATA_ITEM_COUNT == 5) {
        data.add("Noida");
}

but as in your case SAMPLE_DATA_ITEM_COUNT is 6 that's why you don't see anything.

To see all your data, you need to put a for loop around your switch statement - e.g.:

for (int i = 0; i < SAMPLE_DATA_ITEM_COUNT; ++i) {
    switch (i) {

    case 0:
        data.add("New Delhi");
        break;

    case 1:
        data.add("Mumbai");
        break;

    case 2:
        data.add("Kanpur");
        break;

    case 3:
        data.add("Hyderabad");
        break;

    case 4:
        data.add("Bangalore");
        break;

    case 5:
        data.add("Noida");
        break;
    }
}

You could simplify things and get rid of the switch statement and the for loop altogether and just do:

data.add("New Delhi");
data.add("Mumbai");
data.add("Kanpur");
data.add("Hyderabad");
data.add("Bangalore");
data.add("Noida");

Upvotes: 4

Related Questions