title: Saving and retrieving favorite items to firebase-database
body: <p>I am developing an e-commerce android application with firebase-database.
  So i want to add the functionality where a user can add and delete favorite items to and from the database by a click. I wanted like the first click adds to the database and the second removes from the database</p>
  
  <pre><code>This is my firebase structure:
  "Favorites" : {
      "0773047940" : {
        "04" : {
          "description" : "It features a 4.7\" (11.94 cm) display with a screen resolution of 750 x 1334 pixels and runs on iOS v10 operating system. The device is powered by Quad core, 2.37 GHz processor paired with 3 GB of RAM. It has a 2230 mAh battery cell. A rear camera of 13 MP camera, BSI Sensor supporting a resolution of 4128 x 3096 Pixels and the front snapper is powered by a BSI Sensor",
          "discount" : "5",
          "image" : "https://resize.indiatvnews.com/en/centered/newbucket/715_431/2016/07/iphone-7-1468477109.jpg",
          "menuID" : "01",
          "name" : "IPhone 7s",
          "price" : "2100000"
        }
      }
    }
  </code></pre>
  
  <p>This is the productList activity:</p>
  
  <pre><code> database = FirebaseDatabase.getInstance();
          productItemList = database.getReference("Products");
          favoriteItem = database.getReference("Favorites");
  
   private void loadProductItemList(String categoryId) {
          adapter = new FirebaseRecyclerAdapter&lt;Product, ProductViewHolder&gt;(
                  Product.class,
                  R.layout.product_list_layout,
                  ProductViewHolder.class,
                  productItemList.orderByChild("menuid").equalTo(categoryId)//getting product items where menuID equals to category id
          ) {
              @Override
              protected void populateViewHolder(final ProductViewHolder viewHolder, final Product model, final int position) {
                  viewHolder.productItemName.setText(model.getName());
                  Locale locale = new Locale("en", "UG");
                  NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
                  int thePrice = (Integer.parseInt(model.getPrice()));
                  viewHolder.productItemPrice.setText(numberFormat.format(thePrice));
                  Picasso.with(getBaseContext()).load(model.getImage())
                          .into(viewHolder.productItemImage);
  
                  //the name on the toolbar
                  Bundle bundle = getIntent().getExtras();
                  toolbarTitle = bundle.getString("Title_key");
                  setTitle(toolbarTitle);
  
                  final Product productItem = model;
                  viewHolder.setItemClickListener(new ItemClickListener() {
                      @Override
                      public void onClick(View view, int position, boolean isLongClicked) {
                          Intent productDetail = new Intent(ProductList.this, ProductDetail.class);
                          //Getting the category key id and sending it to the product list activity
                          productDetail.putExtra("ProductListID", adapter.getRef(position).getKey());
                          startActivity(productDetail);
                      }
                  });
  
                  //adding to favorite
                  viewHolder.favoriteImage.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          final String itemKey = adapter.getRef(position).getKey();
  
                          favoriteItem.child(Common.user_Current.getPhone()).child(itemKey).addValueEventListener(new ValueEventListener() {
                              @Override
                              public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                  if (dataSnapshot.child(itemKey).equals(itemKey)){
                                      viewHolder.favoriteImage.setImageResource(R.drawable.ic_favorite_black_24dp);
                                      Toast.makeText(ProductList.this, itemKey + "Exists", Toast.LENGTH_SHORT).show();
                                  }
                                  else {
                                      String me = dataSnapshot.getKey();
                                      viewHolder.favoriteImage.setImageResource(R.drawable.ic_favorite_black_24dp);
                                      favoriteItem.child(Common.user_Current.getPhone()).child(itemKey).setValue(model);
                                      Toast.makeText(ProductList.this, model.getName() +"added to favorites", Toast.LENGTH_SHORT).show();
                                  }
                              }
  
                              @Override
                              public void onCancelled(@NonNull DatabaseError databaseError) {
  
                              }
                          });
  
  
                      }
                  });
              }
          };
  
          //Setting the adapter
          recyclerView.setAdapter(adapter);
      }
  </code></pre>
  
  <p>This is the viewHolder</p>
  
  <pre><code>public class ProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
      public TextView productItemName, productItemPrice;
      public ImageView productItemImage, favoriteImage;
      private ItemClickListener itemClickListener;
  
      public ProductViewHolder(@NonNull View itemView) {
          super(itemView);
  
          productItemName = (TextView) itemView.findViewById(R.id.item_name);
          productItemPrice = (TextView) itemView.findViewById(R.id.item_price);
          productItemImage = (ImageView) itemView.findViewById(R.id.item_image);
          favoriteImage = (ImageView) itemView.findViewById(R.id.img_favorite);
          itemView.setOnClickListener(this);
      }
  
      public void setItemClickListener(ItemClickListener itemClickListener) {
          this.itemClickListener = itemClickListener;
      }
  
      @Override
      public void onClick(View view) {
          itemClickListener.onClick(view, getAdapterPosition(), false);
  
      }
  }
  </code></pre>
  
  <p>Please i need help on how to do it in a better way coz am just stuck where i have reached.</p>