Friday, October 24, 2008

There's no place where Eclipse-Databinding doesn't work

Tonight I thought I'll give Android a spin, I download the sources and worked through some samples. After an hour this got boring and because I already managed to get Eclipse-Databinding working in other Environments (GWT, Swing, QT) I thought now it is time to see if I can manage to get a minimal example working on Android.
Here's the application:



The model code looks like this (I'm using UBeans here because they are completely self contained and have no dependencies)

private static class Person extends UBean {
public static final int NAME = 1;

public String getName() {
return (String) get(NAME);
}

public void setName(String name) {
set(NAME, name);
}

@Override
public Object getValueType(int featureId) {
return String.class;
}

};

The UI-Code looks like this (fairly straight forward UI-Code):

TextView view = new TextView(this);
view.setText("Name: ");
TableLayout layout = new TableLayout(this);
layout.addView(view);

EditText text = new EditText(this);
layout.addView(text);

Button button = new Button(this);
button.setText("Say Hello");
button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Dialog dialog = new Dialog(Test.this);
dialog.setTitle("Hello " + p.getName() + "!");
dialog.show();
}

});
layout.addView(button);

And now the important thing how do we connect UI and Model-Attributes? Right we use Eclipse-Databinding (well not the one you get from Eclipse directly because it doesn't compile out of the box but patching it took about 30 minutes :-).

IObservableValue mObs = UBeansObservables.observeValue(realm, p, Person.NAME);
IObservableValue uiObs = AndroidObservables.observeText(realm, text);
DataBindingContext ctx = new DataBindingContext(realm);
ctx.bindValue(uiObs, mObs, null, null);

Cool isn't it? Would a Eclipse-Databinding port for Android help you? Then take a look at the newly proposed Eclipse-Project UFacekit. We already provide Viewer and UI-Observable implementations for different Platforms (SWT,Swing,QT) and plan to provide one for other platforms (GWT, Eclipse-Forms, ... you name it). Why should we not provide them for Android-Widgets too?

1 comment:

Unknown said...

It would be great to have databinding for android widgets!