コード例 #1
0
    @Override
    protected ArrayList<Drink> doInBackground(Void... params) {

      ArrayList<Drink> tempCocktailList = new ArrayList<Drink>();
      ArrayList<Drink> cocktailList = new ArrayList<Drink>();

      String url = "http://www.thecocktaildb.com/api/json/v1/1/filter.php?a=Alcoholic";

      // http://stackoverflow.com/questions/1395551/convert-a-json-string-to-object-in-java
      tempCocktailList = JsonHandler.getJSONFromUrl(url);

      for (Drink tempDrink : tempCocktailList) {
        String id =
            "http://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=" + tempDrink.getIdDrink();
        ArrayList<Drink> drinks = JsonHandler.getJSONFromUrl(id);
        cocktailList.add(drinks.get(0));
      }

      return cocktailList;
    }
コード例 #2
0
    @Override
    protected void onPostExecute(ArrayList<Drink> cocktailList) {

      // IDs
      cocktailIds = new ArrayList<String>();

      // Names
      cocktailNames = new ArrayList<String>();

      // Ingredients still needed
      listIngredientsNeeded = new ArrayList<String>();

      // Number of ingredients still needed
      listNumberIngredientsNeeded = new ArrayList<Integer>();

      // For every possible drink
      for (Drink drink : cocktailList) {

        int numberOfBlanks = 0;
        int numberOfMatches = 0;
        String ingredientsNeeded = "";

        // For every selected ingredient by the user
        for (String ingredient : cocktailIngredients) {

          // This code loops through each ingredient on the cocktail list and checks
          // if the user has any matching ingredients, the results are displayed as
          // user ingredient (number of needed ingredients)

          if (drink.getStrIngredient1().equals(ingredient)) {
            numberOfMatches++;
            if (ingredientsNeeded.equals("")) {
              ingredientsNeeded = drink.getStrIngredient1();
            } else {
              ingredientsNeeded = ingredientsNeeded + ", " + drink.getStrIngredient1();
            }
          } else if (drink.getStrIngredient2().equals(ingredient)) {
            numberOfMatches++;
            if (ingredientsNeeded.equals("")) {
              ingredientsNeeded = drink.getStrIngredient2();
            } else {
              ingredientsNeeded = ingredientsNeeded + ", " + drink.getStrIngredient2();
            }
          } else if (drink.getStrIngredient3().equals(ingredient)) {
            numberOfMatches++;
            if (ingredientsNeeded.equals("")) {
              ingredientsNeeded = drink.getStrIngredient3();
            } else {
              ingredientsNeeded = ingredientsNeeded + ", " + drink.getStrIngredient3();
            }
          } else if (drink.getStrIngredient4().equals(ingredient)) {
            numberOfMatches++;
            if (ingredientsNeeded.equals("")) {
              ingredientsNeeded = drink.getStrIngredient4();
            } else {
              ingredientsNeeded = ingredientsNeeded + ", " + drink.getStrIngredient4();
            }
          } else if (drink.getStrIngredient5().equals(ingredient)) {
            numberOfMatches++;
            if (ingredientsNeeded.equals("")) {
              ingredientsNeeded = drink.getStrIngredient5();
            } else {
              ingredientsNeeded = ingredientsNeeded + ", " + drink.getStrIngredient5();
            }
          } else if (drink.getStrIngredient6().equals(ingredient)) {
            numberOfMatches++;
            if (ingredientsNeeded.equals("")) {
              ingredientsNeeded = drink.getStrIngredient6();
            } else {
              ingredientsNeeded = ingredientsNeeded + ", " + drink.getStrIngredient6();
            }
          } else if (drink.getStrIngredient7().equals(ingredient)) {
            numberOfMatches++;
            if (ingredientsNeeded.equals("")) {
              ingredientsNeeded = drink.getStrIngredient7();
            } else {
              ingredientsNeeded = ingredientsNeeded + ", " + drink.getStrIngredient7();
            }
          } else if (drink.getStrIngredient8().equals(ingredient)) {
            numberOfMatches++;
            if (ingredientsNeeded.equals("")) {
              ingredientsNeeded = drink.getStrIngredient8();
            } else {
              ingredientsNeeded = ingredientsNeeded + ", " + drink.getStrIngredient8();
            }
          } else if (drink.getStrIngredient9().equals(ingredient)) {
            numberOfMatches++;
            if (ingredientsNeeded.equals("")) {
              ingredientsNeeded = drink.getStrIngredient9();
            } else {
              ingredientsNeeded = ingredientsNeeded + ", " + drink.getStrIngredient9();
            }
          } else if (drink.getStrIngredient10().equals(ingredient)) {
            numberOfMatches++;
            if (ingredientsNeeded.equals("")) {
              ingredientsNeeded = drink.getStrIngredient10();
            } else {
              ingredientsNeeded = ingredientsNeeded + ", " + drink.getStrIngredient10();
            }
          }
        }

        // As we need the number of non-matches, we need to take off the ingredients
        // that are actually blank, so no comparison is possible

        if (drink.getStrIngredient1() == null || drink.getStrIngredient1() == "") {
          numberOfBlanks++;
        }
        if (drink.getStrIngredient2() == null || drink.getStrIngredient2() == "") {
          numberOfBlanks++;
        }
        if (drink.getStrIngredient3() == null || drink.getStrIngredient3() == "") {
          numberOfBlanks++;
        }
        if (drink.getStrIngredient4() == null || drink.getStrIngredient4() == "") {
          numberOfBlanks++;
        }
        if (drink.getStrIngredient5() == null || drink.getStrIngredient5() == "") {
          numberOfBlanks++;
        }
        if (drink.getStrIngredient6() == null || drink.getStrIngredient6() == "") {
          numberOfBlanks++;
        }
        if (drink.getStrIngredient7() == null || drink.getStrIngredient7() == "") {
          numberOfBlanks++;
        }
        if (drink.getStrIngredient8() == null || drink.getStrIngredient8() == "") {
          numberOfBlanks++;
        }
        if (drink.getStrIngredient9() == null || drink.getStrIngredient9() == "") {
          numberOfBlanks++;
        }
        if (drink.getStrIngredient10() == null || drink.getStrIngredient10() == "") {
          numberOfBlanks++;
        }

        // 10 possible ingredients - blanks - matches = ingredients needed
        int neededIngredients = 10 - numberOfBlanks - numberOfMatches;

        if (neededIngredients < 3 && numberOfMatches > 1) {
          cocktailIds.add(drink.getIdDrink());
          cocktailNames.add(drink.getStrDrink());
          listNumberIngredientsNeeded.add(neededIngredients);
          listIngredientsNeeded.add(ingredientsNeeded);
        } else {
          // Dont add the drink as it contains no ingredients that match
        }
      }

      mAdapter =
          new CocktailsAdapter(
              getActivity(), cocktailNames, listIngredientsNeeded, listNumberIngredientsNeeded);
      mCocktailList.setAdapter(mAdapter);
      mCocktailList.setOnItemClickListener(
          new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              Log.v(LOG_TAG, "Cocktails adapter pos clicked on:" + position);
              if (Utilities.isNetworkAvailable(getActivity())) {
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("cocktail_id", cocktailIds.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
              } else {
                Log.e(LOG_TAG, "No internet connection");
                Toast.makeText(getActivity(), R.string.no_internet, Toast.LENGTH_LONG).show();
              }
            }
          });
      dialog.hide();
    }