2011-07-21 7 views
2

私のXMLにListviewがあります。そのようなことの構造:Android ListViewレイアウトのパラメータセット

<ListView 
    android:id="@+id/SeriesCastListItemView" 
    android:paddingTop="10dip" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:divider="#FFFFFF" 
    android:dividerHeight="1px"/> 

は、今私は、JavaコードでListviewのための高さを設定します。私は次のコードで試しています。

ListView castListItemView = (ListView) findViewById(R.id.SeriesCastListItemView); 
castListItemView.setAdapter(new CastAdapter(this, castDescriptionArr)); 
castListItemView.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, 500)); 

ただし、これはjava.lang.ClassCastException: android.widget.AbsListView$LayoutParamsです。私のコードの問題は何ですか?

答えて

8

ListViewを含む親ビューのLayoutParamsを使用する必要があります。私の場合、ListViewをフレームレイアウトに配置し、このコードを使用しました。 ListViewをLinearLayoutに配置した場合は、LinearLayout.LayoutParamsを使用します。

ListView castListItemView = (ListView) findViewById(R.id.SeriesCastListItemView); 
castListItemView.setAdapter(new CastAdapter(this, castDescriptionArr)); 
castListItemView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 500)); 

私はそれがあなたの質問のタイプミスだったと仮定しますが、castListItemViewの「C」は、あなたの3行目に小文字されていることを確認してください。

MrJreが良い点を挙げています。多くの異なるビューには独自のLayoutParamsがあるため、以前にインポートしたLayoutParamsクラスに基づいてEclipseを推測するのではなく、(FrameLayout.LayoutParamsのように)使用するパラメータを持つビューを含めることをお勧めします。

詳しくはprevious questionをご覧ください。

+0

LayoutParamsが機能します。しかし、castListItemView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT、500))は、レイアウトの上にあるものを上書きします。リストビューは画像の上にありますが、リストビューの内容は画像上に配置されます。これをどうすれば解決できますか? – Ashwin

+0

@Ashwinあなたが何を意味するか分かりません。コードと別の質問を投稿できますか? – theisenp

0

この行:new ListView.LayoutParams(LayoutParams.FILL_PARENT, 500)あなたはLayoutParamsクラスを混ぜています。

どちらか

new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, 500) 

または

new LayoutParams(LayoutParams.FILL_PARENT, 500) 

は、それが私の推測で修正されます。

+0

いいえ、私はちょうど上記の方法の両方を試みました。それらのどれも働かない。 –

+0

CastListItemView.setLayoutParams()またはcastListItemView.setLayoutParams()? – MrJre

+0

どちらの場合でもこれは機能しません。 – greg7gkb

関連する問題