Last active
February 19, 2017 23:36
-
-
Save Evin1-/8baa591b816c89b64eea73da8d0f4c7d to your computer and use it in GitHub Desktop.
Minimal Realm
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: 'realm-android' | |
android { | |
//... | |
} | |
dependencies { | |
//... | |
} |
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 { | |
//... | |
} | |
dependencies { | |
//... | |
classpath "io.realm:realm-gradle-plugin:2.3.1" | |
} | |
} |
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 MainActivity extends AppCompatActivity { | |
private static final String TAG = "MainActivityTAG_"; | |
private Realm realm; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Realm.init(getApplicationContext()); | |
realm = Realm.getDefaultInstance(); | |
clearValues(); | |
insertValues(); | |
readValues(); | |
} | |
private void clearValues() { | |
realm.beginTransaction(); | |
realm.deleteAll(); | |
realm.commitTransaction(); | |
} | |
private void insertValues() { | |
realm.beginTransaction(); | |
realm.copyToRealm(new User("Juan", 20)); | |
realm.copyToRealm(new User("Miguel", 23)); | |
realm.copyToRealm(new User("Roberto", 39)); | |
realm.copyToRealm(new User("Luis", 19)); | |
realm.copyToRealm(new User("Gaudencio", 25)); | |
realm.commitTransaction(); | |
} | |
private void readValues() { | |
List<User> users = realm.where(User.class).findAll(); | |
for (User user : users) { | |
Log.d(TAG, "readValues: " + user); | |
} | |
} | |
} |
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 extends RealmObject { | |
private String name; | |
private int age; | |
public User() { | |
} | |
public User(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment