title: Insert Custom LinearLayout in ListView Programmatically
body: <p>i know this question is still answered in many posts. and theoretically i know the answer, but in this case i can't get it work.</p>
  
  <p>I have a custom BaseAdapter</p>
  
  <pre><code>@Override
  public Row getView(int position, View convertView, ViewGroup parent)
  {
      ...
  
      Row view = (Row) convertView;
      LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  
      if (view == null)
      {
          view = (JVxDataRow) mInflater.inflate(R.layout.row_base, null);;
          ...
      }
      ...
  }
      return view;
  }
  </code></pre>
  
  <p>this works fine, the row_base.xml is inflated correct, and my list view gets all entries.
  the row_base.xml consist only of:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;mh.android.ui.Row
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/datarow"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:paddingLeft="20dp"
  android:paddingTop="20dp"
  android:paddingBottom="20dp"
  android:paddingRight="20dp" /&gt;
  </code></pre>
  
  <p>therefore i want to remove that file and instead instantiate this programmatically.
  my code for that - which doesn't work - is as following:</p>
  
  <pre><code>@Override
  public Row getView(int position, View convertView, ViewGroup parent)
  {
      ...
  
      Row view = (Row) convertView;
      LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  
      if (view == null)
      {
          view = new Row(context);
          ...
      }
      ...
  }
      return view;
  }
  </code></pre>
  
  <p>to get it work i have to put some layoutparameters in the Row's constructor and moved the inflate from onFinishInflate to the constructor:</p>
  
  <pre><code>public Row(Context context)
  {
      super(context);
      AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);
  
      this.setLayoutParams(params);
      this.setPadding(20, 20, 20, 20);
      this.setOrientation(LinearLayout.VERTICAL);
  
      inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      container = inflater.inflate(R.layout.datarow3, this, true);
  
      this.addView(container);
  }
  </code></pre>
  
  <p>the datarow3.xml looks:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/table_row_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" &gt;
  
  &lt;mh.android.ui.DataItem
      android:id="@+id/dataitem_slot1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:singleLine="true" &gt;
  &lt;/mh.android.ui.DataItem&gt;
  &lt;mh.android.ui.DataItem
      android:id="@+id/dataitem_slot2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:singleLine="true" &gt;
  &lt;/mh.android.ui.DataItem&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>the problem is in the BaseAdapter when i change from inflating the xml to instantiate programmatically, the listview is inside a LinearLayout.</p>
  
  <p>Now the exception:</p>
  
  <pre><code>E/AndroidRuntime(13203): FATAL EXCEPTION: main
  E/AndroidRuntime(13203): java.lang.ClassCastException:     android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
  E/AndroidRuntime(13203):    at android.widget.ListView.measureScrapChild(ListView.java:1170)
  E/AndroidRuntime(13203):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1252)
  E/AndroidRuntime(13203):    at android.widget.ListView.onMeasure(ListView.java:1162)
  E/AndroidRuntime(13203):    at android.view.View.measure(View.java:15288)
  E/AndroidRuntime(13203):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
  E/AndroidRuntime(13203):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
  E/AndroidRuntime(13203):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
  E/AndroidRuntime(13203):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
  E/AndroidRuntime(13203):    at android.view.View.measure(View.java:15288)
  E/AndroidRuntime(13203):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
  E/AndroidRuntime(13203):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  E/AndroidRuntime(13203):    at android.view.View.measure(View.java:15288)
  E/AndroidRuntime(13203):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
  E/AndroidRuntime(13203):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
  E/AndroidRuntime(13203):    at android.view.View.measure(View.java:15288)
  E/AndroidRuntime(13203):    at 
  </code></pre>
  
  <p>what's going wrong there?
  thank's!</p>