Advertisement
Guest User

Untitled

a guest
May 27th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. package id.co.rumahcoding.qaamus;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7.  
  8. import id.co.rumahcoding.qaamus.models.Entry;
  9. import io.realm.Realm;
  10. import io.realm.RealmConfiguration;
  11.  
  12. /**
  13.  * Created by blastocode on 5/28/17.
  14.  */
  15.  
  16. public class Application extends android.app.Application {
  17.     @Override
  18.     public void onCreate() {
  19.         super.onCreate();
  20.  
  21.         //init realm
  22.         Realm.init(this);
  23.         RealmConfiguration realmConfiguration = new RealmConfiguration
  24.                 .Builder()
  25.                 .name(Realm.DEFAULT_REALM_NAME)
  26.                 .deleteRealmIfMigrationNeeded()
  27.                 .schemaVersion(1)
  28.                 .build();
  29.         Realm.setDefaultConfiguration(realmConfiguration);
  30.     }
  31.  
  32.     private void initData() {
  33.         Realm realm = Realm.getDefaultInstance();
  34.  
  35.         // ambil jumlah baris di table Entry
  36.         long count = realm.where(Entry.class).count();
  37.  
  38.         // jika barisnya kosong, maka import dari csv
  39.         if(count == 0) {
  40.             try {
  41.                 InputStream inputStream = getAssets().open("Qaamus.csv");
  42.                 InputStreamReader reader = new InputStreamReader(inputStream);
  43.                 final BufferedReader buffer = new BufferedReader(reader);
  44.  
  45.  
  46.                 realm.executeTransaction(new Realm.Transaction() {
  47.                     @Override
  48.                     public void execute(Realm realm) {
  49.                         long id = 0;
  50.                         String line = "";
  51.  
  52.                         try {
  53.                             while ((line = buffer.readLine()) != null) {
  54.                                 String[] lines = line.split(",");
  55.  
  56.                                 id++;
  57.                                 Entry entry = new Entry();
  58.                                 entry.setId(id);
  59.                                 entry.setIndonesia(lines[0]);
  60.                                 entry.setArab(lines[1]);
  61.  
  62.                                 realm.insert(entry);
  63.                             }
  64.                         }
  65.                         catch (IOException e) {
  66.  
  67.                         }
  68.                     }
  69.                 });
  70.             }
  71.             catch (IOException e) {
  72.  
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement