Моего Сценарий:Checkbox в GroupView в ExpandableList
У меня есть список в groupView и childView расширяемого списка. Элементы GroupView и ChildView имеют флажки. У меня нет проблем с установкой флажков. Я хочу проверить все флажки ChilView, когда установлен флажок в GroupView.
Я прошел через эти два решения:
Android ExpandableListView CheckBox Select Only Clicked Checkbox
и
Check all checkbox in ExpandableListView (Android)
проверить флажок говорят в groupPosition 2, то флажок, скажем groupPosition 5 автоматически получает чек. Опять же, когда я проверяю флажок, скажем в groupPosition 1, тогда флажок в say groupPosition 6 автоматически проверяется. Я сохранил состояние флажка в Hash Map. Может ли кто-нибудь объяснить, почему это происходит.
Моего код для childrenView следующих
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ChildHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.group_item, null);
holder = new ChildHolder();
holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (ChildHolder) convertView.getTag();
}
Item item = getChild(groupPosition, childPosition);
holder.title.setText(item.name);
if (DataHolder.selectedGroupMembers.containsKey(groupPosition) == true)
holder.cb.setChecked(true);
else {
holder.cb.setChecked(false);
}
return convertView;
}
Моего кода для groupView следующих
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final GroupHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.group_list, null);
holder = new GroupHolder();
holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
holder.imageView = (ImageView) convertView
.findViewById(R.id.label_indicator);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.imageView
.setImageResource(groupStatus[groupPosition] == 0 ? R.drawable.group_down
: R.drawable.group_up);
Item groupItem = getGroup(groupPosition);
holder.title.setText(groupItem.name);
if (DataHolder.selectedGroupMembers.containsKey(groupPosition) == true) {
holder.cb.setChecked(true);
} else {
holder.cb.setChecked(false);
}
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked == true) {
DataHolder.selectedGroupMembers.put(groupPosition,
new Boolean(true));
Log.i("Prativa Clicked", "group" + groupPosition);
notifyDataSetChanged();
} else if (DataHolder.selectedGroupMembers
.containsKey(groupPosition)) {
DataHolder.selectedGroupMembers.remove(groupPosition);
Log.i("Prativa Removed ", "group" + groupPosition);
notifyDataSetChanged();
}
}
});
return convertView;
}
Я сохранил состояние хэш карты следующим образом
public static HashMap<Integer,Boolean> selectedGroupMembers = new HashMap<Integer, Boolean>();
Проводка кода помогает много. –
@Prativa, что здесь DataHolder. его строка или объект –
Это статический класс, который содержит данные, используемые во всем приложении. – Prativa