Bind ItemsSource de um enum

22 views
Skip to first unread message

Thiago Lacerda

unread,
Aug 10, 2012, 2:23:25 PM8/10/12
to android...@googlegroups.com
Hi Andy!

   Is that possible?

Best regards

Thiago Lacerda

unread,
Aug 10, 2012, 2:25:10 PM8/10/12
to android...@googlegroups.com
Sorry, i put the title in portuguese! the correct is:
Bind ItemsSource from an enum

Thiago Lacerda Siqueira

unread,
Aug 10, 2012, 3:37:11 PM8/10/12
to android...@googlegroups.com
I've made an implementation using converters:

@SuppressWarnings("rawtypes")
public class ENUMITEMSOURCE extends Converter<ArrayListObservable> {

public ENUMITEMSOURCE(IObservable<?>[] dependents) {
super(ArrayListObservable.class, dependents);
}

@SuppressWarnings({ "unchecked" })
@Override
public ArrayListObservable calculateValue(Object... args) throws Exception {
if (args[0] == null)
return null;
if (!Enum.class.isAssignableFrom(args[0].getClass())) 
return null;
Enum<?> e = (Enum<?>) args[0];
return new ArrayListObservable(e.getClass(), e.getClass().getEnumConstants());
}
}


public class ENUM extends Converter<Integer> {

public ENUM(IObservable<?>[] dependents) {
super(Integer.class, dependents);
}

@Override
public Integer calculateValue(Object... args) throws Exception {
if (args[0] == null)
return null;
if (!Enum.class.isAssignableFrom(args[0].getClass())) 
return null;
Enum<?> e = (Enum<?>) args[0];
return Arrays.asList(e.getClass().getEnumConstants()).indexOf(e);
}
}

and the markup is:

binding:itemSource="ENUMSOURCE(type)"
binding:selectedPosition="ENUM(type)"

where type an observable of my enum type.
what do you think?

PS: I did not find a way to register my custom converters like i do with BindingProviders... i tried to declare it in markup with full path of package ( how is described in http://code.google.com/p/android-binding/wiki/BindingSyntax ), but it not work... Can you help me on this?


Um Abraço,


Thiago Lacerda


2012/8/10 Thiago Lacerda <thia...@gmail.com>

Andy Tsui

unread,
Aug 10, 2012, 10:02:48 PM8/10/12
to android...@googlegroups.com
you can declare your custom converter under gueei.binding.converters package, it still works
Reply all
Reply to author
Forward
0 new messages