I am creating a custom widget, say "CustomWid" in UiBinder.
And in CustomWid.java file I am writing two constructors
one with zero args like
CustomWid(){....}
another with some args like
CustomWid(String a,String b){......}
So,Now I am using my custom widget in another .ui.xml file,in that .ui.xml file
it is working fine when we give
<my:CustomWid/>
alone,
and also fine when we give like
<my:CustomWid a="srt1" b="str2"/>
alone
But "MY PROBLEM" is whenever I am trying to give both the tags in the one .ui.xml as
<my:CustomWid/>
<my:CustomWid a="str1" b="str2"/>
Now it is throwing error when i am using both types of tags in a single .ui.xml
I mean How to use my custom widget tag like a prdefined tag?
I am using @uiConstructor
, but it showing error
Please developers... I need answer as early as possible
UiBinder will only ever use a single constructor for a given widget: either its zero-arg constructor, or a @UiConstructor
(I'm surprised that you say it works when using either one or the other call but not both: one should fail in every case, and one should succeed in every case; if you haven't annotated a constructor with @UiConstructor
, then <my:CustomWid/>
should always work and <my:CustomWid a="str1" b="str2"/>
should always fail)
There are two solutions here:
a
and b
attributes (void setA(String a)
and void setB(String b)
)), and possibly check later (say, in onLoad
or onAttach
) that you have either none or both of A and B, but not one without the other (if that's your rule).@UiField(provided = true)
when you need to use the other constructor (if you choose to have UiBinder use the zero-arg constructor –i.e. no @UiConstructor
–, then that means you'll have to move the a="str1" b="str2"
from the XML to the Java code: @UiField(provided = true) CustomWid myCustomWid = new CustomWid("str1", "str2")
).The first option has my preference.