Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Last active January 26, 2018 19:55
Show Gist options
  • Save alvareztech/70f908d3aebd6488bb84d5ccb497e0dc to your computer and use it in GitHub Desktop.
Save alvareztech/70f908d3aebd6488bb84d5ccb497e0dc to your computer and use it in GitHub Desktop.
Ejemplo base de datos Realm. Curso de desarrollo de aplicaciones Android.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="tech.alvarez.mislibros.AdicionarActivity">
<LinearLayout
android:padding="16dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/tituloEditText"
android:hint="@string/titulo"
android:layout_width="match_parent"
android:layout_height="48dp" />
<EditText
android:id="@+id/autorEditText"
android:hint="@string/autor"
android:layout_width="match_parent"
android:layout_height="48dp" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:onClick="guardarLibro"
android:src="@drawable/ic_save_white_24dp" />
</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="tech.alvarez.mislibros.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/librosRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:onClick="adicionar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add_white_24dp" />
</android.support.design.widget.CoordinatorLayout>
package tech.alvarez.mislibros;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;
import io.realm.Realm;
import tech.alvarez.mislibros.models.Libro;
public class AdicionarActivity extends AppCompatActivity {
public Realm realm;
private EditText tituloEditText;
private EditText autorEditText;
private RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adicionar);
realm = Realm.getDefaultInstance();
tituloEditText = (EditText) findViewById(R.id.tituloEditText);
autorEditText = (EditText) findViewById(R.id.autorEditText);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
}
public void guardarLibro(View view) {
String titulo = tituloEditText.getText().toString();
String autor = autorEditText.getText().toString();
float puntaje = ratingBar.getRating();
realm.beginTransaction();
Libro li = realm.createObject(Libro.class);
li.setTitulo(titulo);
li.setAutor(autor);
li.setPuntaje(puntaje);
realm.commitTransaction();
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
}
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "tech.alvarez.mislibros"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath "io.realm:realm-gradle-plugin:1.1.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorPrimaryText">#DE000000</color>
<color name="colorSecondaryText">#8A000000</color>
<color name="colorDisabledText">#61000000</color>
<color name="colorPrimaryDarkText">#FFFFFF</color>
<color name="colorSecondaryDarkText">#B3FFFFFF</color>
<color name="colorDisabledDarkText">#80FFFFFF</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="@+id/tituloTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/colorPrimaryText"
android:textSize="16sp" />
<TextView
android:id="@+id/autorTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/colorSecondaryText"
android:textSize="14sp" />
<TextView
android:id="@+id/puntajeTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/app_name"
android:textColor="@color/colorAccent"
android:textSize="12sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
package tech.alvarez.mislibros.adapters;
import tech.alvarez.mislibros.models.Libro;
/**
* Created by Daniel Alvarez on 14/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public interface ItemClickOyente {
void onItemClick(Libro libro);
}
package tech.alvarez.mislibros.models;
import io.realm.RealmObject;
/**
* Created by Daniel Alvarez on 13/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class Libro extends RealmObject{
private String titulo;
private String autor;
private float puntaje;
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getAutor() {
return autor;
}
public void setAutor(String autor) {
this.autor = autor;
}
public float getPuntaje() {
return puntaje;
}
public void setPuntaje(float puntaje) {
this.puntaje = puntaje;
}
}
package tech.alvarez.mislibros.adapters;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import io.realm.RealmResults;
import tech.alvarez.mislibros.R;
import tech.alvarez.mislibros.models.Libro;
/**
* Created by Daniel Alvarez on 13/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class LibrosAdapter extends RecyclerView.Adapter<LibrosAdapter.ViewHolder> {
private RealmResults<Libro> dataset;
private ItemClickOyente itemClickOyente;
public LibrosAdapter(RealmResults<Libro> dataset, ItemClickOyente itemClickOyente) {
this.dataset = dataset;
this.itemClickOyente = itemClickOyente;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_libro, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Libro libro = dataset.get(position);
holder.tituloTextView.setText(libro.getTitulo());
holder.autorTextView.setText(libro.getAutor());
holder.puntajeTextView.setText(libro.getPuntaje() + "");
holder.setItemClick(libro, itemClickOyente);
}
@Override
public int getItemCount() {
return dataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView tituloTextView;
private TextView autorTextView;
private TextView puntajeTextView;
public ViewHolder(View itemView) {
super(itemView);
tituloTextView = (TextView) itemView.findViewById(R.id.tituloTextView);
autorTextView = (TextView) itemView.findViewById(R.id.autorTextView);
puntajeTextView = (TextView) itemView.findViewById(R.id.puntajeTextView);
}
public void setItemClick(final Libro libro, final ItemClickOyente itemClickOyente) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemClickOyente.onItemClick(libro);
}
});
}
}
}
package tech.alvarez.mislibros;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import io.realm.Realm;
import io.realm.RealmQuery;
import io.realm.RealmResults;
import io.realm.Sort;
import tech.alvarez.mislibros.adapters.ItemClickOyente;
import tech.alvarez.mislibros.adapters.LibrosAdapter;
import tech.alvarez.mislibros.models.Libro;
public class MainActivity extends AppCompatActivity implements ItemClickOyente{
public Realm realm;
private RecyclerView librosRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
realm = Realm.getDefaultInstance();
librosRecyclerView = (RecyclerView) findViewById(R.id.librosRecyclerView);
librosRecyclerView.setHasFixedSize(true);
librosRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
actualizarDatos();
}
public void adicionar(View view) {
Intent intent = new Intent(this, AdicionarActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
public void actualizarDatos() {
RealmQuery<Libro> query = realm.where(Libro.class);
RealmResults<Libro> results = query.findAll();
results = results.sort("puntaje", Sort.DESCENDING);
librosRecyclerView.setAdapter(new LibrosAdapter(results, this));
}
@Override
public void onItemClick(Libro libro) {
// Toast.makeText(getApplicationContext(), libro.getTitulo(), Toast.LENGTH_SHORT).show();
// realm.beginTransaction();
// libro.setAutor("Maria Loza");
// realm.commitTransaction();
realm.beginTransaction();
libro.deleteFromRealm();
realm.commitTransaction();
actualizarDatos();
}
}
package tech.alvarez.mislibros;
import android.app.Application;
import io.realm.Realm;
import io.realm.RealmConfiguration;
/**
* Created by Daniel Alvarez on 13/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class MisLibrosApp extends Application {
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(realmConfiguration);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment