fitnesskim / tree-view-list-android

Automatically exported from code.google.com/p/tree-view-list-android
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

How to change other nodes state if one node is selected #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

i have a tree view which looks like this

Root
|
+-- Node 1
|  +-- Leaf 1 with Checkbox
|  +-- Leaf 2 with Checkbox
|  +-- Leaf 3 with Checkbox
|
+-- Node 2
|  +-- Leaf 1 with Checkbox
|  +-- Leaf 2 with Checkbox

Now i want to set Leaf 3 disabled (also the Checkbox), if Leaf 1 and 2 are 
selected. 

The idea behind it is that i want to only allow to select a limited number of 
leafs per node.

How would i do this?

Thanks for your help,
 Frank

Original issue reported on code.google.com by frank.eg...@gmail.com on 2 Apr 2011 at 9:31

GoogleCodeExporter commented 9 years ago
One of the solutions:

I'd imagine to do it via some kind of observer pattern. Each view (created 
using getNewView() or updateView() should register tio become observers of the 
selected states (you will need to create your own 
"SelectedStateObserverManager" and let the views register in it. 

Then (as you can see in the example SimpleStandardAdapter which you will have 
to either copy or extend) - you should remember the id of the node (for example 
as tag in checkbox object) which is the location in the tree (from the manager 
you can easily find the parent of given id). You can use it and keep somewhere 
on a side how many children are already selected per parent (or check it 
dynamically). 

Then every time someone makes a selection of a checkbox (you will set 
onCheckChangeListener for it), you could update the count of selected items per 
parent and then fire all the registered observers. Every view could then (in 
the observer notification handling) could check how many of views under the 
parent are already selected. And if there are exactly 2 of them - view would 
make the checkbox disabled (of course only if it is not selected already). 

The same check (how many children of my parent are already selected) will have 
to be done when the view is created or updated (getNewView or updateView in the 
adapter) - so that when you scroll out/in and views are re-created they are 
already disabled/enabled if needed.

Something like that would do the job. 

Original comment by ja...@potiuk.com on 3 Apr 2011 at 9:13