0

I'm trying to return values from Restuarant[] array where the food[i] == "Italian". any ideas how to achieve that?

String Array Name "typeOfFood" is pre-defined as shown below:

    <item>Italian</item>
    <item>Sea Food</item>
    <item>Steak</item>
    <item>Sandwiches</item>
    <item>Steak</item>

private List getRestaurant(){

    List<Restuarant> restaurant_data_list;
    String[] res_name = getResources().getStringArray(R.array.restaurants);
    String[] res_loc = getResources().getStringArray(R.array.locations);
    String[] food = getResources().getStringArray(R.array.typeOfFood);
    String[] cost = getResources().getStringArray(R.array.cost);
    int[] img = {R.drawable.olivegarden,R.drawable.crab,R.drawable.steak,R.drawable.inout,R.drawable.bear};

    //Creating the Shared Preference
    SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    // return selected item id.
    int showFoodTypeId = Integer.parseInt(mySharedPreferences. getString(SELECTED_TYPE, "0"));

    Restuarant[] myRestaurant = new Restuarant[res_name.length];

    switch (showFoodTypeId){

        case 1:

          for (int i = 0; i < res_name.length ; i++) {
                if ( food[i] == "Italian"){
              myRestaurant[i] = new Restuarant(img[i], res_name[i], res_loc[i], food[i], cost[i]);
             }
   }
        break;

        case 2:

            for (int i = 0; i < res_name.length; i++) {
                if (food[i] == "Sea Food"){
                    myRestaurant[i] = new Restuarant(img[i], res_name[i], res_loc[i], food[i], cost[i]);
                }
            }
            break;

        default:

            for (int i = 0; i < res_name.length; i++){
                myRestaurant[i]= new Restuarant(img[i], res_name[i], res_loc[i], food[i], cost[i]);
            }

    }

    restaurant_data_list = new ArrayList<>(Arrays.asList(myRestaurant));
    return restaurant_data_list;

}

Thank you,

Ahmad Sh
  • 1
  • 1

1 Answers1

0

You should loop over food array, not res_name array. Also be sure that you have elements inside img, res_loc, res_name and cost arrays for index i.

for (int i = 0; i < food.length ; i++) {
                if ("Italian".equals(food[i])){
              myRestaurant[i] = new Restuarant(img[i], res_name[i], res_loc[i], food[i], cost[i]);
             }
   }
yılmaz
  • 1,818
  • 13
  • 15