title: Displaying and ArrayList of LinearLayout objects in a ListView by extending ArrayAdapter
body: <p>I have an ArrayList of LinearLayouts.  Each LinearLayout contains several other views that have been programatically added.</p>
  
  <p>What I want to do is display these LinearLayout views in a ListView so to utilise the ability of a ListView to render only what is needed.</p>
  
  <p>My original code used a ScrollView that was defined in the layout xml and i programatically added each of my LinearLayouts to the ScrollView and it worked fine.</p>
  
  <p>I've now replaced the ScrollView in the xml with a ListView and i've added an adapter.</p>
  
  <p>In the example below i've simplified everything to try and get to the root of the issue.  I get a ClassCastException when the getView method of the adapter returns the LinearLayout.</p>
  
  <p>Activity layout xml (trend_scrollable.xml):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" &gt;
          &lt;ListView
                android:id="@+id/trend_listview"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"&gt;
           &lt;/ListView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Layout for each row (simple_chart_item.xml):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/chartItem"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" 
      android:orientation="horizontal" &gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Adapter:</p>
  
  <pre><code>    public class ChartArrayAdapter extends ArrayAdapter&lt;LinearLayout&gt;
      {
          ArrayList&lt;LinearLayout&gt; layoutList;
  
          public ChartArrayAdapter(Context context, ArrayList&lt;LinearLayout&gt; list)
          {
              super(context, R.layout.simple_chart_item, list);
              this.layoutList = new ArrayList&lt;LinearLayout&gt;(list);
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent)
          {
              return layoutList.get(position);
          }
  
          @Override
          public int getCount()
          {
              return this.layoutList != null ? this.layoutList.size() : 0;
          }
      }
  </code></pre>
  
  <p>Activity:</p>
  
  <pre><code>    @Override
  protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      this.setContentView(R.layout.trend_scrollable);
  
      ListView layout = (ListView) findViewById(R.id.trend_listview);
  
      ArrayList&lt;LinearLayout&gt; layoutList = new ArrayList&lt;LinearLayout&gt;();
  
      for (int i = 0; i &lt; 2; i++)
      {
          layoutList.add(getDemoView(GlobalVars.getContext(), i));            
      }
  
      ChartArrayAdapter adapter = new ChartArrayAdapter(this, layoutList);
      layout.setAdapter(adapter);
  }
  </code></pre>
  
  <p>Can someone please help me with this?  I've simplified the getView method of the adapter to always just return a LinearLayout view and i think this means that a new view will always be created.  I suspect maybe there is something wrong with returned LinearView and matching it with "simple_chart_layout.xml", maybe..</p>