カスタムListViewで選択状態を表示させる
行レイアウトとしてCheckedTextViewを追加する
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckedTextView android:layout_width="wrap_content" android:layout_alignParentLeft="true" android:text="" android:id="@+id/checkedSelect" android:layout_height="wrap_content" android:checkMark="?android:attr/listChoiceIndicatorSingle"> </CheckedTextView> <TextView android:text="XXXXXXXXXXXX" android:layout_width="wrap_content" android:layout_toRightOf="@+id/checkedSelect" android:id="@+id/textViewBluetoothAddress" android:layout_height="wrap_content" android:layout_alignTop="@+id/checkedSelect"> </TextView> <TextView android:text="XXXXXXXXXXXXXXXXXXXX" android:layout_below="@+id/textViewBluetoothAddress" android:layout_width="wrap_content" android:id="@+id/textViewBluetoothDeviceName" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textViewBluetoothAddress" android:textSize="16sp"> </TextView> </RelativeLayout>
getView()内でチェックを付ける
CheckedTextView check = (CheckedTextView)row.findViewById(R.id.checkedSelect); check.setChecked(listViewDeviceList.isItemChecked(position));
ボタンがクリックされたら選択されたアイテム情報取得
// なにも選択されていなければキャンセル long[] positions = listViewDeviceList.getCheckItemIds(); if (positions.length == 0) { return; } // 選択されたアイテム取得 BluetoothDeviceInfo item = listData[(int)positions[0]];