title: Android - CardView doesn't show up on RecyclerView connected to Firebase
body: <p>I'm building a <code>RecyclerView</code> in a <code>Fragment</code>, which brings in data from <strong>Firebase</strong> database and suppose to show them on a <code>CardView</code>. I wrote all the code as shown below but all that appears upon running is an empty <code>RecyclerView</code> Fragment with the method <code>getItemCount()</code> returning always 0.</p>
  
  <p><strong>card_item.xml</strong></p>
  
  <pre><code>&lt;android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  android:id="@+id/cardView"
  android:layout_width="match_parent"
  android:layout_height="200dp"
  android:layout_margin="10dp"
  android:orientation="horizontal"
  card_view:cardCornerRadius="5dp"
  card_view:cardElevation="5dp"&gt;
  
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/colorPrimary"&gt;
  
      &lt;TextView
          android:id="@+id/primaryText"
          android:layout_width="300dp"
          android:layout_height="60dp"
          android:textColor="@color/colorPrimary"
          android:textSize="24sp" /&gt;
  
      &lt;TextView
          android:id="@+id/subText"
          android:layout_width="300dp"
          android:layout_height="60dp"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_centerVertical="true"
          android:textColor="@color/colorPrimary"
          android:textSize="14sp" /&gt;
  
      &lt;TextView
          android:id="@+id/rateValue"
          android:layout_width="300dp"
          android:layout_height="60dp"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_below="@+id/subText"
          android:textColor="@color/colorPrimary"
          android:textSize="24sp" /&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>fragment_profile.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#F1F1F1"
  android:orientation="vertical"&gt;
  
  &lt;android.support.v7.widget.RecyclerView
      android:id="@+id/placesRecycler"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>PlacesModel.java</strong></p>
  
  <pre><code>public class PlaceModel {
  private String mPrimaryText, mSubText, mRateValue;
  
  public PlaceModel() {
  }
  
  public PlaceModel(String mCardImage, String mPrimaryText,
                    String mSubText, String mRateValue) {
      //this.mCardImageURL = mCardImage;
      this.mPrimaryText = mPrimaryText;
      this.mSubText = mSubText;
      this.mRateValue = mRateValue;
  
  }
  
   public void setmPrimaryText(String mPrimaryText) {
      this.mPrimaryText = mPrimaryText;
  }
  
  public void setmSubText(String mSubText) {
      this.mSubText = mSubText;
  }
  
  public void setmRateValue(String mRateValue) {
      this.mRateValue = mRateValue;
  }
  
  
  public String getmPrimaryText() {
      return mPrimaryText;
  }
  
  public String getmSubText() {
      return mSubText;
  }
  
  public String getmRateValue() {
      return mRateValue;
  }}
  </code></pre>
  
  <p><strong>PlacesAdapter.java</strong></p>
  
  <pre><code>public class PlacesAdapter extends RecyclerView.Adapter&lt;PlacesAdapter.PlacesViewHolder&gt; {
  
  private ArrayList&lt;PlaceModel&gt; cardContents;
  private Context context;
  
  public PlacesAdapter(Context context, ArrayList&lt;PlaceModel&gt; cardContents) {
      this.cardContents = cardContents;
      this.context = context;
  }
  
  @Override
  public PlacesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(context).inflate(R.layout.card_item, parent, false);
      return new PlacesViewHolder(view);
  }
  
  @Override
  public void onBindViewHolder(PlacesViewHolder holder, int position) {
  
      PlaceModel place = cardContents.get(position);
      holder.primaryText.setText(place.getmPrimaryText());
      holder.subText.setText(place.getmSubText());
      holder.rateValue.setText(place.getmRateValue());
  }
  
  
  @Override
  public int getItemCount() {
      return cardContents.size();
  }
  
  public class PlacesViewHolder extends RecyclerView.ViewHolder {
  
      CardView cardView;
      public TextView primaryText, subText, rateValue;
  
  
      public PlacesViewHolder(View itemView) {
          super(itemView);
  
  
          cardView = (CardView) itemView.findViewById(R.id.cardView);
          primaryText = (TextView) itemView.findViewById(R.id.primaryText);
          subText = (TextView) itemView.findViewById(R.id.subText);
          rateValue = (TextView) itemView.findViewById(R.id.rateValue);
  
      }
  
  }}
  </code></pre>
  
  <p><strong>FirebaseConnector.java</strong></p>
  
  <pre><code>  public class FirebaseConnector {
  DatabaseReference db;
  PlaceModel placeModel = new PlaceModel();
  ArrayList&lt;PlaceModel&gt; cardContent = new ArrayList&lt;&gt;();
  
  
  public FirebaseConnector(DatabaseReference db) {
      this.db = db;
  
  }
  
  public ArrayList&lt;PlaceModel&gt; retrieve() {
  
      db.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
              fetchData(dataSnapshot);
          }
  
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
              fetchData(dataSnapshot);
          }
  
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
  
          }
  
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
      return cardContent;
  }
  
  private void fetchData(DataSnapshot dataSnapshot) {
      cardContent.clear(); //clear card content from last usage
  
      for (DataSnapshot ds : dataSnapshot.getChildren()) {
  
          placeModel = ds.child("Place Model").getValue(PlaceModel.class);
          cardContent.add(placeModel);
       }
  
   }}
  </code></pre>
  
  <p><strong>ProfileFragment.java</strong></p>
  
  <pre><code>public class ProfileFragment extends Fragment {
  // TODO: Rename parameter arguments, choose names that match
  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  private static final String ARG_PARAM1 = "param1";
  private static final String ARG_PARAM2 = "param2";
  /////////////////////////////////////////////////////////////
  private FirebaseAuth mAuth;
  private FirebaseAuth.AuthStateListener mAuthListener;
  private RecyclerView placesRecycler;
  private PlacesAdapter placesAdapter;
  private FirebaseConnector connector;
  private DatabaseReference ref;
  
  
  // TODO: Rename and change types of parameters
  private String mParam1;
  private String mParam2;
  
  
  public ProfileFragment() {
      // Required empty public constructor
  }
  
  
  // TODO: Rename and change types and number of parameters
  public static ProfileFragment newInstance(String param1, String param2) {
      ProfileFragment fragment = new ProfileFragment();
      Bundle args = new Bundle();
      args.putString(ARG_PARAM1, param1);
      args.putString(ARG_PARAM2, param2);
      fragment.setArguments(args);
      return fragment;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      if (getArguments() != null) {
          mParam1 = getArguments().getString(ARG_PARAM1);
          mParam2 = getArguments().getString(ARG_PARAM2);
      }
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View v = inflater.inflate(R.layout.fragment_profile, container, false);
      mAuth = FirebaseAuth.getInstance();
      //Initialize Database..
      ref = FirebaseDatabase.getInstance().getReference();
      connector = new FirebaseConnector(ref);
  
      //Initialize RecyclerView
      placesRecycler = (RecyclerView)v.findViewById(R.id.placesRecycler);
      placesRecycler.setLayoutManager(new LinearLayoutManager(this.getActivity()));
      //Adapter
      placesAdapter = new PlacesAdapter(this.getActivity(), connector.retrieve());
      placesRecycler.setAdapter(placesAdapter);
  
      Toast.makeText(getActivity(), "We have "+placesAdapter.getItemCount()+" cards", Toast.LENGTH_LONG).show();
  
  
      return v;
  }
  
  @Override
  public void onStart() {
      super.onStart();
      if (mAuth.getCurrentUser() == null) {
          startActivity(new Intent(getActivity(), LoginActivity.class));
      }
  }
  
  public void updateUI() {
  
      if (mAuth.getCurrentUser() == null) {
          startActivity(new Intent(getActivity(), LoginActivity.class));
      }
  
  }}
  </code></pre>
  
  <p><strong>Database trial structure</strong>
  <a href="https://i.stack.imgur.com/w5Qg1.png" rel="nofollow noreferrer">Database trial structure</a></p>