Created
September 22, 2016 09:57
-
-
Save benznest/dd106f3bcb46b136e365e53370533b0e to your computer and use it in GitHub Desktop.
Intro to Realm.io workshop project.
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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:padding="16dp" | |
tools:context="com.benzneststudios.myrealmproject.MainActivity"> | |
<Button | |
android:id="@+id/btn_generate" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:padding="16dp" | |
android:text="Generate 100 students" | |
android:textSize="18sp"/> | |
<EditText | |
android:id="@+id/edt_data" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:inputType="textMultiLine" | |
/> | |
</LinearLayout> |
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.benzneststudios.myrealmproject; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import io.realm.Realm; | |
import io.realm.RealmResults; | |
public class MainActivity extends AppCompatActivity { | |
private Realm realm; | |
Button btnGenerate; | |
EditText edtData; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
realm = Realm.getDefaultInstance(); | |
btnGenerate = (Button) findViewById(R.id.btn_generate); | |
btnGenerate.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
generateStudent(); | |
} | |
}); | |
edtData = (EditText) findViewById(R.id.edt_data); | |
} | |
private void showStudent() { | |
RealmResults<Student> listStudent = getAllStudent(); | |
Log.d("STUDENT", "SIZE = " + listStudent.size()); | |
String str = ""; | |
for (Student student : listStudent) { | |
str += "\nID = " + student.getStudentId() + | |
" , " + student.getStudentName() + | |
" , (" + student.getStudentScore() + ")"; | |
edtData.setText(str); | |
} | |
} | |
private void generateStudent(){ | |
clearStudent(); | |
insertStudent(); | |
} | |
private void insertStudent() { | |
final ArrayList<Student> listStudentGenerate = getSampleStudentData(100); | |
realm.executeTransactionAsync( | |
new Realm.Transaction() { | |
@Override | |
public void execute(Realm realm) { | |
for (final Student s : listStudentGenerate) { | |
Student student = realm.createObject(Student.class); | |
student.setStudentId(s.getStudentId()); | |
student.setStudentName(s.getStudentName()); | |
student.setStudentScore(s.getStudentScore()); | |
Log.d("STUDENT", "Add student id = " + s.getStudentId()); | |
} | |
} | |
}, new Realm.Transaction.OnSuccess() { | |
@Override | |
public void onSuccess() { | |
Toast.makeText(getApplicationContext(), "Generate student complete.", Toast.LENGTH_SHORT).show(); | |
showStudent(); | |
} | |
}, new Realm.Transaction.OnError() { | |
@Override | |
public void onError(Throwable error) { | |
error.printStackTrace(); | |
Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
private void clearStudent() { | |
realm.beginTransaction(); | |
realm.delete(Student.class); | |
realm.commitTransaction(); | |
} | |
public ArrayList<Student> getSampleStudentData(int size) { | |
String name[] = {"Raj Koothrappali", "Penny Hofstadter", "Leonard Hopstater", "Sheldon cooper", "Howard Wolowitz"}; | |
ArrayList<Student> listStudent = new ArrayList<>(); | |
for (int i = 1; i <= size; i++) { | |
Student student = new Student(); | |
student.setStudentId(i); | |
student.setStudentName(name[i % name.length]); | |
student.setStudentScore((int) (Math.random() * 100)); | |
listStudent.add(student); | |
} | |
return listStudent; | |
} | |
public RealmResults<Student> getAllStudent() { | |
RealmResults<Student> result = realm.where(Student.class).findAll(); | |
return result; | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
realm.close(); | |
} | |
private void updateStudent(){ | |
final Student student = new Student(); | |
student.setStudentId(1); | |
student.setStudentName("Benznest"); | |
realm.executeTransaction(new Realm.Transaction() { | |
@Override | |
public void execute(Realm realm) { | |
realm.copyToRealmOrUpdate(student); | |
} | |
}); | |
} | |
} |
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.benzneststudios.myrealmproject; | |
import android.app.Application; | |
import io.realm.Realm; | |
import io.realm.RealmConfiguration; | |
/** | |
* Created by benznest on 22-Sep-16. | |
*/ | |
public class MyApplication extends Application{ | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
initRealm(); | |
} | |
private void initRealm() { | |
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this) | |
.name("android.realm") | |
.schemaVersion(0) | |
.deleteRealmIfMigrationNeeded() | |
.build(); | |
Realm.setDefaultConfiguration(realmConfiguration); | |
} | |
} |
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.benzneststudios.myrealmproject; | |
import io.realm.RealmObject; | |
import io.realm.annotations.PrimaryKey; | |
import io.realm.annotations.Required; | |
/** | |
* Created by benznest on 22-Sep-16. | |
*/ | |
public class Student extends RealmObject { | |
@PrimaryKey | |
int studentId; | |
@Required | |
String studentName; | |
int studentScore; | |
public int getStudentId() { | |
return studentId; | |
} | |
public void setStudentId(int studentId) { | |
this.studentId = studentId; | |
} | |
public String getStudentName() { | |
return studentName; | |
} | |
public void setStudentName(String studentName) { | |
this.studentName = studentName; | |
} | |
public int getStudentScore() { | |
return studentScore; | |
} | |
public void setStudentScore(int studentScore) { | |
this.studentScore = studentScore; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment