Last active
November 29, 2015 12:02
-
-
Save Gnzlt/d557718a39aab6a5f10a to your computer and use it in GitHub Desktop.
GDG Córdoba Android Study Jam - Simple Realm Demo
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
package com.gdgcordoba.demorealm; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.TextView; | |
import io.realm.Realm; | |
public class MainRealmActivity.java extends AppCompatActivity { | |
private Realm realm; | |
private TextView tv_nombre; | |
private TextView tv_edad; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
realm = Realm.getInstance(this); | |
tv_nombre = (TextView) findViewById(R.id.nombre); | |
tv_edad = (TextView) findViewById(R.id.edad); | |
findViewById(R.id.readButton).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
leerPersona(); | |
} | |
}); | |
findViewById(R.id.writeButton).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
escribirPersona(); | |
} | |
}); | |
} | |
private void leerPersona() { | |
Persona personaLeida = realm.where(Persona.class) | |
.findFirst(); | |
if (personaLeida != null) { | |
tv_nombre.setText("Nombre: " + personaLeida.getNombre()); | |
tv_edad.setText("Edad: " + personaLeida.getEdad()); | |
} | |
} | |
private void escribirPersona() { | |
Persona personaEscrita = new Persona(); | |
personaEscrita.setNombre("Caca"); | |
personaEscrita.setEdad(18); | |
realm.beginTransaction(); | |
realm.copyToRealm(personaEscrita); | |
realm.commitTransaction(); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
realm.close(); | |
} | |
} |
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
package com.gdgcordoba.demorealm; | |
import io.realm.RealmObject; | |
public class Persona extends RealmObject { | |
private String nombre; | |
private int edad; | |
public Persona() { | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public int getEdad() { | |
return edad; | |
} | |
public void setEdad(int edad) { | |
this.edad = edad; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment