private ArrayList<Favorite> getSelectedFavorites() {
   ArrayList<Favorite> selectedFavorites = new ArrayList<>();
   List<Favorite> favorites = favoritesSelectionAdapter.getItems();
   for (Favorite favorite : favorites) {
     if (favorite.isSelected()) {
       selectedFavorites.add(favorite);
     }
   }
   return selectedFavorites;
 }
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = this.getArguments();
    position = bundle.getInt("position", 0);
    int favoriteId = bundle.getInt("favoriteId", 0);
    favoriteIsSelected = bundle.getBoolean("favoriteIsSelected", false);

    db = new DatabaseHelper(activity);
    favorite = db.getFavorite(favoriteId);

    MaterialDialog dialog =
        new MaterialDialog.Builder(activity)
            .title(R.string.edit_favorite)
            .customView(R.layout.add_edit_favorite_dialog, true)
            .positiveText(R.string.dialog_positive_text)
            .negativeText(android.R.string.cancel)
            .onPositive(
                new MaterialDialog.SingleButtonCallback() {
                  @Override
                  public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    inputName = input.getText().toString().trim();
                    if (iconPath == 0) {
                      iconPath = favorite.getFavoriteImage();
                    }
                    favorite.setFavoriteImage(iconPath);
                    favorite.setFavoriteName(inputName);
                    favorite.setSelected(favoriteIsSelected);
                    db.updateFavorite(favorite);
                    db.closeDB();

                    reloadHost();
                  }
                })
            .build();

    favoriteImage = (ImageView) dialog.findViewById(R.id.favorite_image);

    int iconName = favorite.getFavoriteImage();
    if (iconName == 0) {
      iconName = R.drawable.image_favorites;
    }
    favoriteImage.setImageResource(iconName);
    favoriteImage.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            favoriteImages = new FavoriteImages();

            View view = activity.getLayoutInflater().inflate(R.layout.icon_selector_grid, null);
            GridView gridView = (GridView) view.findViewById(R.id.icons_grid_view);
            gridView.setAdapter(new IconAdapter(activity, favoriteImages.getFavoriteImagesIds()));
            gridView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    favoriteImage.setImageResource(favoriteImages.getFavoriteImageId(position));
                    iconPath = favoriteImages.getFavoriteImageId(position);
                    positiveAction.setEnabled(true);
                    gridDialog.dismiss();
                  }
                });
            gridDialog = new MaterialDialog.Builder(activity).customView(gridView, false).build();

            gridDialog.show();
          }
        });

    input = (EditText) dialog.findViewById(R.id.favorite_name);
    input.requestFocus();
    input.setText(favorite.getFavoriteName());

    positiveAction = dialog.getActionButton(DialogAction.POSITIVE);
    input.addTextChangedListener(new OnTextChange(positiveAction));

    positiveAction.setEnabled(false);

    return dialog;
  }