使用this Expandable List checkbox example code作爲基線,我試圖保存並保持複選框狀態。然而,隨機複選框正在被選中或取消選中(觸發我的OnCheckedChangeListener
以及新值),當我將它們滾動到視線之外時,最小化它們的組,甚至最小化/最大化附近的組!Android,複選框隨機選中/未選中可擴展列表
public Object getChild(int groupPosition, int childPosition) {
return colors.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return (long)(groupPosition*1024+childPosition); // Max 1024 children per group
}
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = null;
if(convertView != null) {
v = convertView;
} else {
v = inflater.inflate(R.layout.child_row, parent, false);
}
Color c = (Color)getChild(groupPosition, childPosition);
TextView color = (TextView)v.findViewById(R.id.childname);
if(color != null) {
color.setText(c.getColor());
}
TextView rgb = (TextView)v.findViewById(R.id.rgb);
if(rgb != null) {
rgb.setText(c.getRgb());
}
CheckBox cb = (CheckBox)v.findViewById(R.id.check1);
cb.setChecked(c.getState());
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
colors.get(groupPosition).get(childPosition).setState(isChecked);
context.setColorBool(groupPosition, childPosition, isChecked);
Log.d("ElistCBox2", "listitem position: " +groupPosition+"/"+childPosition+" "+isChecked);
}
});
return v;
}
我不知道這段代碼可能是什麼原因造成的,所以我們歡迎任何關於包含在這裏的建議。我的代碼只有在嘗試保存值時才與原始代碼不同。
我對ExpandableListViews中的CheckBox有這個確切的問題。一直在尋找答案。希望你的問題得到比我更多的迴應。 – jmease
@jmease今天我評論了我的convertView代碼,並且問題消失了。我仍然有點不確定是否一切正常,我覺得應該使用ExpandableListView之外的東西來重寫UI,但代碼正在運行,所以我現在可能會保留原樣。我強烈建議你嘗試忽略convertview,看看它是否適合你。 –