已经有2天了,我开始对编程能力失去希望
这是我的BaseAdapter
:
private Context ctx;
private ArrayList<MultiSelectionItem> items;
private LayoutInflater layoutInflater;
private boolean[] checked;
// Constructor
public MultiSelectionSpinnerAdapter(Context ctx, ArrayList<MultiSelectionItem> items) {
this.ctx = ctx;
this.items = items;
this.layoutInflater = LayoutInflater.from(ctx);
checked = new boolean[items.size()];
for (int i = 0; i < checked.length; i++) {
checked[i] = true; // Notice I'm setting all checkboxes to checked !
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final MultiSelectionItem item = items.get(position);
CheckBox checkBox = (CheckBox) convertView;
if (checkBox == null) {
checkBox = (CheckBox) layoutInflater.inflate(R.layout.multi_selection_checkbox, parent, false);
}
checkBox.setText(item.getLabel());
checkBox.setTag(Integer.valueOf(position));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// isChecked is always false !!!!
checked[(Integer) buttonView.getTag()] = isChecked;
}
});
checkBox.setChecked(checked[position]);
return checkBox;
}
CheckBoxes状态在checked
数组中正确存储, HOWEVER ...
即使我做checkBox.setChecked(true)
,监听器中的isChecked
参数也总是包含false!那是为什么?
CheckBox布局:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:ellipsize="marquee" />
答案 0 :(得分:1)
您的适配器非常好,如果您的ListView
项目只是CheckBox
或任何Checkable
,那么如果ListView
正在使用{{}},您可以遇到此问题1}}或listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE)
。
修复很简单,只是不要使用任何选择模式或使用xml中的android:choiceMode="singleChoice"
或Java代码中的"multiChoice"
。
但是,如果您将此适配器与CHOICE_MODE_MULTIPLE
一起使用,则必须通过放置Spinner
来欺骗Spinner
您没有任何Checkable
根元素在其他一些布局中,如下所示:
CheckBox
这种方式<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:singleLine="true" />
</FrameLayout>
会在Spinner
上调用setChecked()
,默认情况下,检查状态不会通过UI层次结构传播,因此一切都会按预期工作,您可以选择多个FrameLayout
内的项目下拉。
答案 1 :(得分:0)
在将值设置为Checkbox
之前,将侦听器设为null。
checkBox.setOnCheckedChangeListener(null);
checkBox.setChecked(checked[position]);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// isChecked is always false !!!!
checked[(Integer) buttonView.getTag()] = isChecked;
}
});
//Your code ............