private void update(ArrayList<Category> newItems) {
    ArrayList<Category> arrayForRemoving = new ArrayList<>();
    for (int i = 0; i < publishItems.size(); i++) {
      Category category = publishItems.get(i);
      if (!newItems.contains(category)) {
        arrayForRemoving.add(category);
      }
    }
    for (Category category : arrayForRemoving) {
      publishItems.remove(category);
    }

    for (Category category : newItems) {
      if (publishItems.indexOf(category) == SortedList.INVALID_POSITION) {
        publishItems.add(category);
      }
    }
    if (publishItems.size() == 0) {
      if (currentFilter.equals(FILTER_ALL)) {
        callback.onEmpty(
            R.string.empty_no_categories_restart_app,
            R.drawable.ic_menu_gallery,
            R.string.button_restart_app,
            null);
      } else {
        callback.onEmpty(
            R.string.empty_no_favorite_categories,
            R.drawable.ic_menu_gallery,
            R.string.button_show_all_categories,
            null);
      }
    }
  }
  @Override
  public void onBindViewHolder(final ViewHolder holder, int position) {
    Category category = publishItems.get(position);

    ImageLoader.getInstance()
        .displayImage(category.getImageUrl(), holder.image, UIL.getImageOptions());

    holder.name.setText(category.getName());

    if (category.isFavorite()) {
      holder.favorite.setImageResource(R.mipmap.ic_star_amber_24dp);
    } else {
      holder.favorite.setImageResource(R.mipmap.ic_star_outline_amber_24dp);
    }
    holder.favorite.setTag(category);
    holder.favorite.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (!(v.getTag() instanceof Category))
              throw new RuntimeException("Category in tag instanceof error");
            Category shop = (Category) v.getTag();
            shop.setFavorite(!shop.isFavorite());
            notifyItemChanged(publishItems.indexOf(shop));
            shop.updateOrAddToDB();
            callback.onFavoriteChanged(shop);
          }
        });

    holder.setClickListener(
        new ViewHolder.ClickListener() {
          @Override
          public void onClick(View v, int position) {
            callback.onItemClick(v, position);
          }
        });
  }
Example #3
0
 public Departure getItem(int position) {
   return departures.get(position);
 }