ListView に情報を動的に追加しようとしています。追加する情報は、「デバイス名」(メイン項目)と「MAC アドレス」(サブ項目)です。オンラインの例を以下に示します。注: 項目 1 をデバイス 1 の名前に置き換え、サブ項目 1 をデバイス 1 の MAC アドレスに置き換えたいと考えています。これは、デバイスがスキャンされるときにリストが読み込まれるため、動的に実行する必要があります。
.
これが繰り返しとしてマークされる前に、次の質問を見ましたが、助けにはなりませんでした: AndroidにListView サブアイテム テキストを追加する、 ListView にサブアイテムを追加する方法 、 ListViewにアイテムとサブアイテムを追加する方法
これらの質問を読んで得た結論は、カスタムを実装してメソッドArrayAdapter
をオーバーライドする必要があるということです。getView()
2 つのテキスト ビューを含むカスタム レイアウトを作成しました。
cyan_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/main_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/cyan"/>
<TextView
android:id="@+id/sub_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/dark_cyan"/>
</LinearLayout>
次に、Activity クラスでカスタム ArrayAdapter を作成しようとしましたが、public View getView(final int position, View convertView, ViewGroup parent)
メソッドに何を入れればよいかわかりません。さらに、テキストビュー サブアイテムを追加するだけの場合、カスタム ArrayAdapter を作成する必要がありますか?