ApiDemos のcom.example.android.apis.view.List11の例を見てきました。この例では、各行はビューandroid.R.simple_list_item_multiple_choiceを取ります。このような各ビューにはTextView
と がありCheckBox
ます。
TextView
ここで、各ビューに 2と 1を持たせたいと思います。List3の例CheckBox
と似ています。次のようなカスタム レイアウト ファイル row.xml を作成してみました。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="@+id/checkbox"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/text_name"
android:textSize="13px"
android:textStyle="bold"
android:layout_toLeftOf="@id/checkbox"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_phone"
android:textSize="9px"
android:layout_toLeftOf="@id/checkbox"
android:layout_below="@id/text_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
次に、Activity
では、次のonCreate()
ようにします。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Query the contacts
mCursor = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row,
mCursor,
new String[] { Phones.NAME, Phones.NUMBER},
new int[] { R.id.text_name, R.id.text_phone });
setListAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
結果は私が望むもののように見えますが、リストはどの項目が選択されているかを認識していないようです。また、 を正確にクリックする必要がありますCheckBox
。List11 の例では、項目の行をクリックするだけです。
では、各行のカスタム ビューで複数選択リストを作成するには、どうすればよいでしょうか? どうもありがとう。