title: ListView and programmatically margin for rows
body: <p>I'm retrieving data from WebService and I use a custom adapter <code>ArrayAdapter</code> to populate my <code>listView</code>. For each row, I have to set a left or right margin depending webservice return. So my idea was using this following code :</p>
  
  <pre><code>LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                          LinearLayout.LayoutParams.MATCH_PARENT,
                          LinearLayout.LayoutParams.WRAP_CONTENT);
  layoutParams.setMargins(30, 0, 0, 0);  // left margin
  holder.mainContainer.setLayoutParams(layoutParams);  // Here is the problem
  </code></pre>
  
  <p>mainContainer is a LinearLayout</p>
  
  <p>In <code>getView</code> method I use the view hodler pattern</p>
  
  <pre><code>MessageHolder holder = null;
  
  if (convertView == null) {
      convertView = LayoutInflater.from(MessagesActivity.this).inflate(R.layout.thread_activity_listview_item, null);
  
      holder = new MessageHolder();
      holder.mainContainer = (LinearLayout) convertView.findViewById(R.id.main_container);
  
      convertView.setTag(holder);
  } else {
      holder = (MessageHolder) convertView.getTag();
  }
  </code></pre>
  
  <p>Here is my <code>xml</code> for a row : </p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/main_container"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/rounded_left_corner_discussion_on"
  android:orientation="vertical"
  android:padding="10dp" &gt;
  
  &lt;ImageView
      android:id="@+id/image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="5dp"
      android:layout_marginRight="15dp"
      android:layout_marginTop="5dp"
      android:gravity="center_vertical" /&gt;
  
  &lt;TextView
      android:id="@+id/content"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="2dp"
      android:layout_marginTop="2dp"
      android:gravity="center_vertical"
      android:textColor="@android:color/black"
      android:textSize="16sp" /&gt;
  
  &lt;LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="right"
      android:layout_marginBottom="2dp"
      android:layout_marginTop="2dp"
      android:orientation="horizontal" &gt;
  
      &lt;TextView
          android:id="@+id/updated"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="12sp" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>But with this code, I have the following error :</p>
  
  <pre><code>java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
  </code></pre>
  
  <p>I precise, I can't use padding in my situation. Moreover if I use <code>AbsListView.LayoutParams</code> instead of <code>LinearLayout.LayoutParams</code>, I can't set margin anymore.</p>
  
  <p>Thanks</p>