title: How can I add CardView dynamically in RecyclerView?
body: <p>So is my first time implementing a RecyclerView. </p>
  
  <p>I am trying to retrieve data from a database, then instantiate a class called <strong>Service</strong> which will be used to create a CardView dynamically to be displayed in a RecyclerView as an item. The RecyclerView is part of a Fragment class. </p>
  
  <p>The Fragment class: </p>
  
  <pre><code>package com.alsowaygh.getitdone.view.services;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v7.widget.CardView;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.TextView;
  
  import com.alsowaygh.getitdone.R;
  import com.alsowaygh.getitdone.controller.service.QueryServices;
  import com.alsowaygh.getitdone.controller.service.Service;
  
  import java.util.ArrayList;
  
  public class ServicesListFragment extends Fragment {
      private RecyclerView mRecyclerView;
      private RecyclerView.Adapter mAdapter;
      private CardView mServiceCardView;
      private RecyclerView.LayoutManager mLayoutManager;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View rootView = inflater.inflate(R.layout.services_list_fragment, container, false);
  
          mRecyclerView = rootView.findViewById(R.id.services_recycler_view);
          mServiceCardView = rootView.findViewById(R.id.service_card_view);
          mLayoutManager = new LinearLayoutManager(rootView.getContext());
          mRecyclerView.setLayoutManager(mLayoutManager);
  
          //retreive argument from CategoriesListFragment (selected)
          String selectedCategory = getArguments().getString("category");
  
          //Query services for selected category
          QueryServices queryServices = new QueryServices();
          ArrayList&lt;Service&gt; services = queryServices.getServicesForCategory(selectedCategory);
  
  
          mAdapter = new ServiceListAdapter(services);
          mRecyclerView.setAdapter(mAdapter);
  
          return rootView;
      }
  
  }
  </code></pre>
  
  <p>Fragment Layout includes Recycler View:</p>
  
  <pre><code>&lt;LinearLayout android:id="@+id/categories_list_fragment"
      android:name="com.alsowaygh.getitdone.view.services.ServicesListFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginBottom="8dp"
      android:layout_marginTop="8dp"
      xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
      &lt;android.support.v7.widget.RecyclerView
          android:id="@+id/services_recycler_view"
          android:scrollbars="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/&gt;
  
  &lt;/LinearLayout&gt; 
  </code></pre>
  
  <p>Adapter Class this is where I am stuck and the code isn't complete: </p>
  
  <pre><code>package com.alsowaygh.getitdone.view.services;
  
  import android.support.annotation.NonNull;
  import android.support.v7.widget.CardView;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.alsowaygh.getitdone.R;
  import com.alsowaygh.getitdone.controller.service.Service;
  
  import java.util.ArrayList;
  
  public class ServiceListAdapter extends RecyclerView.Adapter {
  
  
      private ArrayList&lt;Service&gt; mServices;
  
      public static class ViewHolder extends RecyclerView.ViewHolder{
  
          public ViewHolder(View itemView) {
              super(itemView);
          }
      }
  
      public ServiceListAdapter(ArrayList&lt;Service&gt; services){
          mServices = services;
      }
  
      @NonNull
      @Override
      public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          // create a new view
          CardView v = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.list_item_service, parent, false);
      }
  
      @Override
      public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
  
      }
  
      @Override
      public int getItemCount() {
          return 0;
      }
  }
  </code></pre>
  
  <p>Layout including the CardView which must be added to RecyclerView</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
          android:id="@+id/service_card_view"
          android:layout_width="match_parent"
          android:layout_height="140dp"
          android:layout_gravity="center"
          card_view:cardBackgroundColor="@color/defaultActivityBackground"
          card_view:cardCornerRadius="10dp"
          card_view:contentPadding="5dp"&gt;
  
          &lt;TextView
              android:id="@+id/title"
              style="@style/item_list_service_title_text_style"
              android:layout_width="221dp"
              android:layout_height="40dp"
              android:layout_gravity="top"
              android:layout_marginLeft="10dp"
              android:gravity="center_vertical"
              android:text="Service Title" /&gt;
  
          &lt;TextView
              android:id="@+id/providerName"
              style="@style/item_list_service_detail_text_style"
              android:layout_width="221dp"
              android:layout_height="40dp"
              android:layout_gravity="center_vertical"
              android:layout_marginLeft="10dp"
              android:gravity="center_vertical"
              android:text="Provider Name" /&gt;
  
          &lt;TextView
              android:id="@+id/price"
              style="@style/item_list_service_detail_text_style"
              android:layout_width="221dp"
              android:layout_height="38dp"
              android:layout_gravity="bottom"
              android:layout_marginLeft="10dp"
              android:gravity="center_vertical"
              android:text="Price" /&gt;
  
          &lt;ImageView
              android:id="@+id/imageView"
              android:layout_width="141dp"
              android:layout_height="140dp"
              android:layout_gravity="right"
              android:tint="@color/colorAccent"
              android:background="@android:color/white"
              app:srcCompat="@drawable/service_image_placeholder" /&gt;
      &lt;/android.support.v7.widget.CardView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I don't know how to put things together to embed CardView dynamically inside RecyclerView </p>
  
  <p>Your help is much appreciated..</p>