Last active
August 31, 2015 10:13
-
-
Save up1/2d1a692eaf7e3ae061a5 to your computer and use it in GitHub Desktop.
Android Data Binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
classpath "com.android.tools.build:gradle:1.4.0-beta1" | |
classpath "com.android.databinding:dataBinder:1.0-rc1" | |
} | |
} | |
allprojects { | |
repositories { | |
jcenter() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
apply plugin: 'com.android.databinding' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<layout xmlns:android="http://schemas.android.com/apk/res/android"> | |
<data> | |
<variable | |
name="user" | |
type="com.example.somkiat.databinding.model.User" /> | |
</data> | |
<RelativeLayout > | |
<EditText | |
android:id="@+id/firstname" | |
android:text="@{user.firstName}"/> | |
<EditText | |
android:id="@+id/lastname" | |
android:text="@{user.lastName}"/> | |
</RelativeLayout> | |
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); | |
User defaultUser = new User(); | |
defaultUser.setFirstName("Somkiat"); | |
defaultUser.setLastName("Puisungnoen"); | |
binding.setUser(defaultUser); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void updateUIWithNewData(User user) { | |
EditText firstName = (EditText)findViewById(R.id.firstname); | |
EditText lastName = (EditText)findViewById(R.id.lastname); | |
firstName.setText(user.getFirstName()); | |
lastName.setText(user.getLastName()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class User { | |
private String firstName; | |
private String lastName; | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment