title: Android recyclerView, how to specify a row layout
body: <p>I'm tying to use RecyclerView in my app. i'm following and trying to adapt the code on the developer website. here.</p>
  
  <p><a href="https://developer.android.com/training/material/lists-cards.html" rel="nofollow noreferrer">Tutorial</a></p>
  
  <p>I cannot workout how the row layout is set in the Adapter.I'll post my code below but the following line:</p>
  
  <pre><code>TextView startDate = (TextView) LayoutInflater.from(parent.getContext())
                      .inflate(R.layout.get_rota_row_layout, parent, false);
  </code></pre>
  
  <p>.</p>
  
  <p>is giving me the following exception:</p>
  
  <pre><code>&gt; java.lang.ClassCastException: android.widget.LinearLayout cannot be
  &gt; cast to android.widget.TextView
  &gt;                                                                                              at
  &gt; loneworker.carefreegroup.com.loneworker.LoneworkerGetRotaActivityFragment$MyAdapter.onCreateViewHolder(LoneworkerGetRotaActivityFragment.java:161)
  &gt;                                                                                              at
  &gt; loneworker.carefreegroup.com.loneworker.LoneworkerGetRotaActivityFragment$MyAdapter.onCreateViewHolder(LoneworkerGetRotaActivityFragment.java:135)
  &gt;                                                                                              at
  &gt; android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6073)
  &gt;                                                                                              at
  &gt; android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5243)
  &gt;                                                                                              at
  &gt; android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5153)
  &gt;                                                                                              at
  &gt; android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
  &gt;                                                                                              at
  &gt; android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
  &gt;                                                                                              at
  &gt; android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
  &gt;                                                                                              at
  &gt; android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
  &gt;                                                                                              at
  &gt; android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3374)
  &gt;                                                                                              at
  &gt; android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3183)
  &gt;                                                                                              at
  &gt; android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3627)
  &gt;                                                                                              at android.view.View.layout(View.java:18799)
  </code></pre>
  
  <p>.</p>
  
  <p>I have used ListViews and recyclerViews before but i have not done it this way before. On the above tutorial from the Android dev website, i cannot see where the cardView is set.</p>
  
  <p>.</p>
  
  <pre><code>public class ActivityFragment extends Fragment {
  
      private static final String TAG = LoneworkerGetRotaActivityFragment.class.getSimpleName();
      AppObj appObj;
      List&lt;Visit&gt; mVisitList;
      String URL;
      String companyID;
      String ID2;
      String userID;
  
      private RecyclerView mRecyclerView;
      private RecyclerView.Adapter mAdapter;
      private RecyclerView.LayoutManager mLayoutManager;
  
  
  
  
  
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          appObj = (AppObj)getActivity().getApplication();
          URL = appObj.dbModel.getCompanyUrl();
          Log.e(TAG, "URL = " + URL);
          companyID = appObj.dbModel.getCompanyID();
          ID2 = appObj.dbModel.getCompanyWebServiceGuid();
          userID = appObj.dbModel.getUserID();
  
  
  
      }//end of onCreate
  
  
  
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
  
          LayoutInflater lf = getActivity().getLayoutInflater();
          View view = lf.inflate(R.layout.l_w_get_rota_activity_fragment, container, false);
  
          mRecyclerView = (RecyclerView) view.findViewById(R.id.get_rota_recycler_view);
  
  
  
          sendRequest();
  
          return view;
  
  
      }
  
  
  
  
      private void sendRequest(){
  
          //https://CFTest.carefreeapp.co.uk/roadrunner.asmx/GetRotaHours?CarerID=36&amp;RotaDate=2-Nov-2017
          //https://CFTest.carefreeapp.co.uk/roadrunner.asmx/RRGetRota?ID1=900&amp;ID2=9d817606-9194-4528-84b4-028e5aa90330&amp;CarerID=36&amp;RotaDate=2-Nov-2017
  
          StringRequest stringRequest = new StringRequest(URL + "/roadrunner.asmx/RRGetRota?ID1=" + companyID + "&amp;ID2=" + ID2 + "&amp;CarerID=" + userID + "&amp;RotaDate=" + "2-Nov-2017",
                  new Response.Listener&lt;String&gt;() {
                      @Override
                      public void onResponse(String response) {
                          //success - parse rota
  
                          DomParser dp = new DomParser(getActivity());
                          mVisitList = dp.parseUserRota(response);
                          Log.e(TAG, "mVisitList has size of " + mVisitList.size());
  
                          // use this setting to improve performance if you know that changes
                          // in content do not change the layout size of the RecyclerView
                          mRecyclerView.setHasFixedSize(true);
  
                          // use a linear layout manager
                          mLayoutManager = new LinearLayoutManager(getActivity());
                          mRecyclerView.setLayoutManager(mLayoutManager);
  
                          // specify an adapter (see also next example)
                          mAdapter = new MyAdapter(mVisitList);
                          mRecyclerView.setAdapter(mAdapter);
  
  
  
  
                      }
                  },
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG).show();
                      }
                  });
  
  
          RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
          requestQueue.add(stringRequest);
      }
  
  
  
  
  
  
      public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; {
  
  
          // Provide a reference to the views for each data item
          // Complex data items may need more than one view per item, and
          // you provide access to all the views for a data item in a view holder
          public class ViewHolder extends RecyclerView.ViewHolder {
              // each data item is just a string in this case
              public TextView tvStartDate;
              public ViewHolder(TextView v) {
                  super(v);
                  tvStartDate = v;
              }
          }
  
          // Provide a suitable constructor (depends on the kind of dataset)
          public MyAdapter(List&lt;Visit&gt; myVisitList) {
              mVisitList = myVisitList;
          }
  
          // Create new views (invoked by the layout manager)
          @Override
          public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                         int viewType) {
              // create a new view
              TextView startDate = (TextView) LayoutInflater.from(parent.getContext())
                      .inflate(R.layout.get_rota_row_layout, parent, false);
              // set the view's size, margins, paddings and layout parameters
  
              ViewHolder vh = new ViewHolder(startDate);
              return vh;
          }
  
          // Replace the contents of a view (invoked by the layout manager)
          @Override
          public void onBindViewHolder(ViewHolder holder, int position) {
              // - get element from your dataset at this position
              // - replace the contents of the view with that element
              holder.tvStartDate.setText(mVisitList.get(position).getStartDate());
  
          }
  
          // Return the size of your dataset (invoked by the layout manager)
          @Override
          public int getItemCount() {
              return mVisitList.size();
          }
      }
  
  
  
  
  }//end of class
  </code></pre>
  
  <p>.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"&gt;
  
      &lt;!-- A CardView that contains a TextView --&gt;
  
              &lt;android.support.v7.widget.CardView
              xmlns:card_view="http://schemas.android.com/apk/res-auto"
              android:id="@+id/card_view"
              android:layout_gravity="center"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              card_view:cardCornerRadius="4dp"&gt;
  
                  &lt;TextView
                      android:id="@+id/get_rota_startdate"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content" /&gt;
  
  
              &lt;/android.support.v7.widget.CardView&gt;
  
  
  
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
      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:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      &gt;
  
      &lt;android.support.v7.widget.RecyclerView
          android:id="@+id/get_rota_recycler_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:scrollbars="vertical" /&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>