Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package id.co.rumahcoding.qaamus;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import id.co.rumahcoding.qaamus.models.Entry;
- import io.realm.Realm;
- import io.realm.RealmConfiguration;
- /**
- * Created by blastocode on 5/28/17.
- */
- public class Application extends android.app.Application {
- @Override
- public void onCreate() {
- super.onCreate();
- //init realm
- Realm.init(this);
- RealmConfiguration realmConfiguration = new RealmConfiguration
- .Builder()
- .name(Realm.DEFAULT_REALM_NAME)
- .deleteRealmIfMigrationNeeded()
- .schemaVersion(1)
- .build();
- Realm.setDefaultConfiguration(realmConfiguration);
- }
- private void initData() {
- Realm realm = Realm.getDefaultInstance();
- // ambil jumlah baris di table Entry
- long count = realm.where(Entry.class).count();
- // jika barisnya kosong, maka import dari csv
- if(count == 0) {
- try {
- InputStream inputStream = getAssets().open("Qaamus.csv");
- InputStreamReader reader = new InputStreamReader(inputStream);
- final BufferedReader buffer = new BufferedReader(reader);
- realm.executeTransaction(new Realm.Transaction() {
- @Override
- public void execute(Realm realm) {
- long id = 0;
- String line = "";
- try {
- while ((line = buffer.readLine()) != null) {
- String[] lines = line.split(",");
- id++;
- Entry entry = new Entry();
- entry.setId(id);
- entry.setIndonesia(lines[0]);
- entry.setArab(lines[1]);
- realm.insert(entry);
- }
- }
- catch (IOException e) {
- }
- }
- });
- }
- catch (IOException e) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement