how can we set Height of list view in java file?
Hi I am fetching data for List View from web and want to set Height of List View according to data no of elements in List View are not fix, so I am trying below code but its gives error at run time, so any one can help me to solve this problem, thanks
you can see my xml file here
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/sos_scroll"
android:layout_above="@+id/last_lin" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="@+id/date"
android:layout_marginTop="5dip" android:fadingEdge="none"
android:scrollbars="none">
<RelativeLayout android:layout_width="fill_parent"
android:id="@+id/sos_rel" android:layout_height="wrap_content"
android:orientation="vertical">
<ListView android:paddingLeft="1dip" android:scrollbars="none"
android:cacheColorHint="#00000000" android:focusable="true"
android:id="@+id/list" android:dividerHeight="2dip"
android:layout_below="@+id/date" android:footerDividersEnabled="false"
android:headerDividersEnabled="false" android:layout_marginRight="12dip"
and开发者_运维技巧roid:listSelector="@android:color/transparent"
android:background="@drawable/newshape_list"
android:layout_marginLeft="12dip" android:paddingRight="1dip"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:fadingEdge="none" android:divider="#A9ABAD"></ListView>
<RelativeLayout android:layout_width="wrap_content"
android:layout_marginLeft="10dip" android:layout_marginBottom="5dip"
android:layout_marginRight="10dip" android:layout_height="wrap_content"
android:layout_below="@+id/list">
<TableLayout android:layout_width="wrap_content"
android:id="@+id/add_table" android:layout_centerHorizontal="true"
android:background="@drawable/listback" android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow android:id="@+id/add_data">
<ImageView android:layout_width="wrap_content"
android:src="@drawable/addbuttonimage" android:id="@+id/que1"
android:layout_height="wrap_content"></ImageView>
<TextView android:text="Add New Stress..."
android:layout_width="wrap_content" android:textStyle="bold"
android:textColor="#000000" android:layout_marginLeft="5dip"
android:textSize="15sp" android:layout_height="wrap_content"
android:padding="3dip" />
</TableRow>
</TableLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
<LinearLayout android:id="@+id/last_lin"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingBottom="52dip" android:paddingTop="2dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button android:layout_width="wrap_content" android:textColor="#FFFFFF"
android:id="@+id/done" android:textStyle="bold" android:text="Save"
android:layout_height="wrap_content" android:background="@drawable/savebutton"></Button>
</LinearLayout>
error is here
08-22 11:00:34.930: ERROR/AndroidRuntime(324): Uncaught handler: thread main exiting due to uncaught exception
08-22 11:00:34.959: ERROR/AndroidRuntime(324): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1291)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1238)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:279)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:299)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:893)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.ScrollView.onMeasure(ScrollView.java:276)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:569)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:361)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:569)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:361)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.os.Looper.loop(Looper.java:123)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at java.lang.reflect.Method.invoke(Method.java:521)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-22 11:00:34.959: ERROR/AndroidRuntime(324): at dalvik.system.NativeStart.main(Native Method)
Have you thought about creating your ListView completely dynamically? In other words, try programmatically creating your ListView, and then manually add it to your layout.
Also, is there any reason in particular that you want to specify the ListView height manually? Your ListView will automatically resize itself anyway depending on the amount of data you have, and if you have more data than that could fit on the screen, it will become scrollable.
Edit
In reference to your second comment and your included XML layout, here's what I suggest you do.
Your XML file should only have one root element (hence why it's called the root). Currently, you have a ScrollView and a LinearLayout. I suggest you just use a LinearLayout as the root. You can use this as a rough skeleton:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
... >
<ListView
...
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1" />
<TableLayout
...
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="0" />
<Button
...
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="0" />
</LinearLayout>
This layout basically pushes your Button to the very bottom, and your TableLayout to the bottom, but above the Button. Your ListView will take the rest of the space above the the TableLayout. (By the way, a TableRow is basically a specialized LinearLayout. You can simply replace the TableLayout with a horizontal LinearLayout to achieve similar results, unless you plan to add multiple rows to the table and need equal sizing for the columns.)
精彩评论