title: Change row height programmatically in ListView
body: <p>I have a ListView and I need to change the height of the rows based on the height of the ListView. That's because, I would like to show only a fixed number of rows at a time but, given the multiple screens problem, I need to compute the height programmatically.</p>
  
  <p>The problem is, when <code>getView()</code> method of my custom adapter returns the view, android arises an exception:</p>
  
  <blockquote>
    <p>07-21 15:17:00.391: E/AndroidRuntime(620): FATAL EXCEPTION: main
    07-21 15:17:00.391: E/AndroidRuntime(620): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.AdapterView.addView(AdapterView.java:471)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.LayoutInflater.inflate(LayoutInflater.java:497)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at us.hucare.adapters.ShelfAdapter.getView(ShelfAdapter.java:72)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.AbsListView.obtainView(AbsListView.java:2033)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.ListView.makeAndAddView(ListView.java:1772)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.ListView.fillDown(ListView.java:672)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.ListView.fillFromTop(ListView.java:732)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.ListView.layoutChildren(ListView.java:1625)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.AbsListView.onLayout(AbsListView.java:1863)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.View.layout(View.java:11278)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.View.layout(View.java:11278)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.View.layout(View.java:11278)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.View.layout(View.java:11278)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.View.layout(View.java:11278)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.View.layout(View.java:11278)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.View.layout(View.java:11278)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.View.layout(View.java:11278)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.View.layout(View.java:11278)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.os.Handler.dispatchMessage(Handler.java:99)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.os.Looper.loop(Looper.java:137)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at android.app.ActivityThread.main(ActivityThread.java:4424)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at java.lang.reflect.Method.invokeNative(Native Method)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at java.lang.reflect.Method.invoke(Method.java:511)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    07-21 15:17:00.391: E/AndroidRuntime(620):  at dalvik.system.NativeStart.main(Native Method)</p>
  </blockquote>
  
  <p>Here is the first part of the <code>getView()</code></p>
  
  <pre><code>if (convertView == null || mDataChanged) {
          mDataChanged = false;
          LayoutInflater inflater = (LayoutInflater) mContext
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = inflater.inflate(R.layout.row_shelf, parent);
          LayoutParams params = convertView.getLayoutParams();
          if (params == null) { 
              params = new LayoutParams(LayoutParams.MATCH_PARENT, mShelfHeight); 
          } else {
              params.height = mShelfHeight;
          }
          convertView.setLayoutParams(params);
      }
  </code></pre>
  
  <p>I think the problem should be in that lines because if I execute this code below:</p>
  
  <pre><code>if (convertView == null || mDataChanged) {
              mDataChanged = false;
              LayoutInflater inflater = (LayoutInflater) mContext
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = inflater.inflate(R.layout.row_shelf, null);
  
          }
  </code></pre>
  
  <p>It works normally. (But it shows more rows than I want)</p>
  
  <p>I've found lots of answer on this problem, but no one seems work for me.
  Any suggestion please?</p>