我有2个问题。
第一个问题。
我有一个 CheckedTextView“全选”项目和一个在 MutipleChoice 的适配器中设置的数组项目列表。我目前正在尝试通过选择 CheckedTextView 将数组项的所有复选框的状态设置为 true/false。它可以工作,但有一个错误。这样当数组的最后一项为真时,“SelectAll”为真将起作用,但如果数组的最后一项为假,它将取消选中所有项。我想要的是即使任何项目的状态为假,当checkedtextview 选择为真时,它也会设置为真。
第二个问题。
当所有数组项的状态为真时,我似乎无法将 checkedtextview 设置为真。
会不会是我做错了?给予帮助将不胜感激..
这是我的 CheckedTextView “全选”的方法
private void markAll (Boolean state) {
for (int i = 0; i < lv.getCount(); i++) {
lv.setItemChecked(i, state);
}
}
这是用于检查数组项状态的编码
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
for(int i = 0; i < lv.getCount(); i++) {
//if even 1 of the item is false, the checkedtextview will be set to false
if(lv.isItemChecked(i) == false) {
ctv.setChecked(false);
}
//if all of the item is true, the checkedtextview will be set to true as well
//the coding should be done in the if-else below, if i'm not wrong
if(...) {
}
}
}
编辑
这是调用我的方法 ctv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ctv.setClickable(true);
if(ctv.isPressed() && ctv.isChecked() == false) {
ctv.setChecked(true);
markAll(true);
}
else {
ctv.setChecked(false);
markAll(false);
}
}
});
这是我的checkedtextview的xml代码
<CheckedTextView
android:id="@+id/checkedtext"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:text="Select all"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:clickable="true"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"/>