android


Android incorrect behavior of child CheckBox


I have a custom ExpendableListViewAdapter. Like this:
Group
Child
Child
Child
Group
All his groups and childs have checkboxes.
I want parent CheckBox to dynamically check itself when all his children are checked.
How to implement this?
I've implemented selection of children then the parent element selected.
This is my method for it:
void selectGroup(boolean isSelected) {
for (Item itemChild : item.getItems()) {
if (isSelected) itemChild.isSelected = true;
else itemChild.isSelected = false;
itemChild.setSelected(isSelected);
}
notifyDataSetChanged();
}
And I summon it inside getGroupView:
holder.cbChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
item.isSelected = isChecked;
item.setSelected(isChecked);
if (getChildrenCount(groupPosition) != 0) {
selectGroup(item.isSelected);
}
}
});
And it works fine.
I've tried to implement parent selection as well.
Method:
public boolean isAllChildrenSelected(int groupPosition) {
int selectedNumber = 0;
for (int i = 0; i < getChildrenCount(groupPosition); i++) {
Item itemChild = getChild(groupPosition, i);
if (itemChild.isSelected) {
selectedNumber++;
}
}
if (selectedNumber == getChildrenCount(groupPosition)) {
notifyDataSetChanged();
return true;
} else return false;
}
And I summon this method inside my getGroupView as well:
if (getChildrenCount(groupPosition) != 0) {
if (isAllChildrenSelected(groupPosition)) {
holder.cbChild.setChecked(true);
}
}
And then, in getChildView inside setOnCheckedChangeListener for child CheckBox I summon notifyDataSetChanged();.
But I'm getting some strange behavior - for example, if group have 3 childs and I'm trying to select the 1st one, it selects the 2nd and the 3rd.
I think, this is because I putted notifyDataSetChanged(); in a wrong place, but I cannot figure out where exactly and how to fix it.
Well, it looks like I've figured it out.
Add this method:
private void selectGroupIfAllChildrenSelected() {
int numSelected = 0;
for (Child child: group.getChildrenList()) {
if (child.isSelected()) {
numSelected++;
}
if (group.getChildrenList().size() == numSelected) {
group.setSelected(true);
} else if (numSelected == 0) {
group.setSelected(false);
}
}
notifyDataSetChanged();
}
And summon it inside your getChildView method inside OnCheckedChangeListener:
holder.cbChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
child.setSelected(isChecked);
selectGroupIfAllChildrenSelected();
}
});
Also, don't forget to set to null your setOnCheckedChangeListener inside getChildView before you set a CheckBox state, otherwise you will get some strange behaviour:
holder.cbChild.setOnCheckedChangeListener(null);
holder.cbChild.setChecked(child.isSelected());
All this will also deselect your group CheckBox as soon as you deselect all the children.

Related Links

PhoneGap Build for windows phone and other platforms
Swipe tabs below ActionBar with based on Fragments
Linux Kernel wait_for_completion_timeout not wakeup by complete
Viewing a PDF file in Android
How does the OnTouch event behave?
socialauth android ResourceNotFound
Remove recent app button as done in SureLock application
How to Dynamically check if XML contains UI Element
alarm triggers even after finish time in android
Deleting item from ListView and Refreshing it
layout edittext cast webview
Can I use paypal for android app which charges for video upload service
Opening a new activity: animation starts but activity is showed before the animation
Synchronizing partial database model from server to client
Webview with intent filters
Upload of MediaFiles is always returning NullPointException

Categories

HOME
xml
scikit-learn
adal
c99
requirements
square
performancepoint
heap
android-securityexception
mautic
datastage
pybossa
symmetricds
cumulocity
heat
bitnami
esoteric-languages
job-scheduling
null-pointer
android-7.0-nougat
digital-signature
cfml
swagger-2.0
charles
drive
articulate-storyline
guzzle
jquery-ias
ember-cli
dataflow-diagram
exploit
xcode-ui-testing
beautifier
surveymonkey
testcomplete
panoramas
hibernate-validator
contacts
joomla2.5
least-squares
hivemq
openerp-7
sim-card
verifiable-c
diacritics
mongoose-schema
xv6
skylink
phpdbg
robomongo
configurationmanager
twilio-click-to-call
tf
qtranslate
oracleclient
arules
monkey
shinobi
donations
pure-css
flex4.6
beamer
drawstring
bizagi
phpwebsocket
uiactionsheet
jparsec
toran-proxy
grgit
intel-c++
google-books
zen-cart
toolkit
canalyzer
android-cookiemanager
viewer
pseudo-class
webfinger
appserver
lambda-architecture
codeplex
square-bracket
artemis
insert-id
windows-98
twisted.web
preload
sqlclr
curb
android-loadermanager
jdb
koken
cgimage
screen-size
catransform3d
lr
event-receiver
cuteeditor
django-paypal
codebase
collectionviewsource
interprocess
lxr
windows-identity
application-planning
soappy
information-management

Resources

Encrypt Message