我正在尝试在列表视图中设置我的复选框的选中状态,这是代码:
mShalatAdapter = new ArrayAdapter<String>(getActivity(),R.layout.pengaturan_list_item, R.id.label_shalat, waktuShalat);
mListShalat = (ListView) v.findViewById(R.id.pengaturan_checkbox);
mListShalat.setAdapter(mShalatAdapter);
CheckBox cb;
TextView txv;
for (int i = 0; i < mListShalat.getCount(); i++) {
View vListSortOrder = mListShalat.getAdapter().getView(i, null, null);
txv = (TextView) vListSortOrder.findViewById(R.id.label_shalat);
cb = (CheckBox) vListSortOrder.findViewById(R.id.status_shalat);
String labelShalat = txv.getText().toString();
Toast.makeText(getActivity(), labelShalat, Toast.LENGTH_SHORT).show();
if (cb.isChecked() == false) {
cb.setChecked(true);
}
}
这是我的pengaturan_list_item.xml
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="@+id/label_shalat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="Label Ibadah"
android:textColor="@android:color/white"
android:textSize="20dp" />
<CheckBox
android:id="@+id/status_shalat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/ibadah_checkbox_list"
android:visibility="visible" />
</LinearLayout>
我得到了 TextView 和 CheckBox 的值,但我不知道为什么 CheckBox 没有在设备中显示真实状态。请帮助,提前谢谢。
我的 ibadah_checkbox_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true"
android:drawable="@drawable/checkbox_ibadah_checked" />
<item android:state_checked="false"
android:drawable="@drawable/checkbox_ibadah_unchecked" />
</selector>