0

リストアイテムに2行のテキストがあるリストを作成する必要があります。TwoLineListItemカスタムリストアイテムの作成を開始しましたが、その後、コンポーネントを発見しました。私はこのコードを書きました:

pageFilterResultView=new TwoLineListItem(containerActivity);
pageFilterResultView.getText1().setText("Test");

ただし、getText1nullを返し、2行目は。をスローしNullPointerExceptionます。だから私はコンストラクターの代わりに膨らんだレイアウトを使う必要があると思いました。TwoLineListItemのドキュメントでは、レイアウトにリソースを使用できると指定されてandroid.R.layout.two_line_list_itemいるため、コードを次のように変更しました。

LayoutInflater inflater=(LayoutInflater)containerActivity.
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pageFilterResultView=(TwoLineListItem)inflater.inflate(android.R.
        layout.two_line_list_item,null);
pageFilterResultView.getText1().setText("Test");

ただし、ClassCastExceptionレイアウトは実際には。であるため、これはをスローしますLinearLayoutTwoLineListItemから継承するRelativeLayoutため、階層内の上位クラスにレイアウトをキャストすることもできません。

だから問題は:どうすればTwoLineListItem正しく使用できますか?独自のカスタムレイアウトを作成する必要がありますか?もしそうなら、私がまだ自分でリストアイテムを作成するすべての作業をしなければならない場合、このコンポーネントのポイントは何ですか?

4

2 に答える 2

1

TwoLineListItemを正しく使用するにはどうすればよいですか?

TwoLineListItemウィジェットは、TextViewsあなたが提供しなければならない2つ以上のファサードです。TwoLineListItem'行でを使用するには、特定のid(および)を持つ2つの(少なくとも)子を持つウィジェットListViewがある行レイアウトが必要です。このようなもの:TwoLineListItemTextViewandroid.R.id.text1android.R.id.text2

<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@android:id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_below="@android:id/text1"/>

</TwoLineListItem>

次に、次のgetView()ようにメソッドで使用できます。

pageFilterResultView=(TwoLineListItem)inflater.inflate(R.layout.the_layout_file_above,null);
pageFilterResultView.getText1().setText("Test");

もちろん、含まれているレイアウトファイルを(必要なIDをTwoLineListItem持つ2つがある限り)の子として使用することもできます。TextViews

<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <!-- the android version of the two line layout -->
    <include layout="@android:layout/two_line_list_item" />

</TwoLineListItem>

ただし、これはレイアウトの深さを増すだけなので、避ける必要があります。

もしそうなら、私がまだ自分でリストアイテムを作成するすべての作業をしなければならない場合、このコンポーネントのポイントは何ですか?

このウィジェットをプログラムで使用できないという事実から判断すると、このコンポーネントの必要性もわかりません。

于 2012-10-05T13:46:45.137 に答える
-1

レイアウトコードをClassCastExceptionを使用してtrycatchブロック内に配置すると、友だちは正常に機能します。....。

于 2012-10-05T13:07:26.063 に答える