wealthfront/magellan

Using data binding

Closed this issue ยท 2 comments

Hi, i'm trying to use data binding with Magellan but i can't get it to work.

If anyone can give me a hint on how to do this i will be very grateful ๐Ÿ˜„

This is my code:

MainActivity.java

public class MainActivity extends SingleActivity {

    @Override
    protected Navigator createNavigator() {
        return Navigator.withRoot(new HomeScreen()).build();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }
}

PresenceScreen.java

public class PresenceScreen extends Screen<PresenceView> {

    @Override
    protected PresenceView createView(Context context) {
        return new PresenceView(context);
    }

}

PresenceView.java

public class PresenceView extends BaseScreenView<PresenceScreen> {

    public PresenceView(Context context) {
        super(context);
        ScreenPresenceBinding binding = DataBindingUtil.setContentView(getScreen().getActivity(), R.layout.screen_presence); // This throws NullPointerException at getScreen()

        // also tried:
        // ScreenPresenceBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.screen_presence, this, false);
        // with this the screen is never rendered 
    }

}

screen_presence.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <!-- ... -->
    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">

        <!-- ... -->

    </LinearLayout>

</layout>
    ScreenPresenceBinding binding = DataBindingUtil.setContentView(getScreen().getActivity(), R.layout.screen_presence);

IMHO, you don't want to use setContentView but inflate into that <com.wealthfront.magellan.ScreenContainer/> that you defined in the layout of your single activity

So that would be

ScreenPresenceBinding binding = ScreenPresenceBinding.inflate(LayoutInflater.from(context), this, true)

@jmfayard it worked, thank you!
I've tried inflating but not this way. It would be cool to have an example of data binding for newbies like me.
Again, thank you!