コード例 #1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mSelectedRecipes = new ArrayList<>();

    setContentView(R.layout.activity_meal_recipe_add);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    setUpActionBar();

    // Unpack recipes
    mVisibleRecipes = new ObservableArrayList<>();
    mRecipes = unpackRecipes();
    mVisibleRecipes.addAll(mRecipes);
    mSelectedRecipes.addAll(existRecipes());

    // Initialize view
    final ListView list = (ListView) findViewById(R.id.recipe_list);
    final RecipeAddListAdapter adapter =
        new RecipeAddListAdapter(this, mVisibleRecipes, mSelectedRecipes);
    adapter.setAddListener(
        new RecipeAddListAdapter.RecipeAddListener() {
          @Override
          public void recipeAddRequested(Recipe recipe) {

            Log.d(TAG, "Added recipe " + recipe);
            // Add to list of recipes to add
            if (!mSelectedRecipes.contains(recipe)) {
              mSelectedRecipes.add(recipe);
              try {
                if (!App.getAccessor().containsLocalRecipe(recipe.getObjectId())) {
                  App.getAccessor().storeRecipe(recipe);
                }
              } catch (SQLException e) {
                new AlertDialog.Builder(MealRecipeAddActivity.this)
                    .setTitle("Failed to process recipe")
                    .setMessage(e.getLocalizedMessage())
                    .show();
              }
            }
            updateResult();
          }
        });
    list.setAdapter(adapter);

    mSearchView = (SearchView) findViewById(R.id.search);
    mSearchView.setOnQueryTextListener(new SearchHandler());

    // Set initial result
    updateResult();
  }
コード例 #2
0
  /**
   * Update the meals shown on screen. Shows filtered meals if the user types in the search bar or
   * shows all the meals if nothing is entered
   */
  private void updateVisibleMeals() {
    final String query = mSearchView.getQuery().toString();
    // Make all meals visible
    mVisibleRecipes.clear();
    if (!query.isEmpty()) {
      final List<Recipe> filteredRecipes = new ArrayList<>();
      // Limit mVisibleMeals to the meals whose titles contain the query
      // Case insensitive
      final String lowerQuery = query.toLowerCase(Locale.getDefault());

      for (Recipe recipe : mRecipes) {
        final String lowerTitle = recipe.getTitle().toLowerCase(Locale.getDefault());
        if (lowerTitle.contains(lowerQuery)) {
          filteredRecipes.add(recipe);
        }
      }
      mVisibleRecipes.addAll(filteredRecipes);
    } else {
      // Empty query
      mVisibleRecipes.addAll(mRecipes);
    }
  }
コード例 #3
0
  private void searchRecipes() {
    final String query = mSearchView.getQuery().toString();
    Set<Recipe> recipeSet = new HashSet<>();
    recipeSet.addAll(mRecipes);
    List<Recipe> recipes = null;

    try {
      recipes = App.getAccessor().loadRecipes(query);
    } catch (SQLException e) {
      e.printStackTrace();
    }
    recipeSet.addAll(recipes);
    mRecipes.clear();
    mRecipes.addAll(recipeSet);
  }