SHARE
TWEET

Untitled

a guest Nov 19th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package id.com.rumahcoding.sekolahku
  2.  
  3. import android.content.ContentValues
  4. import android.content.Context
  5. import android.database.Cursor
  6. import android.database.sqlite.SQLiteDatabase
  7. import id.com.rumahcoding.sekolahku.model.Siswa
  8. import java.util.ArrayList
  9.  
  10. class SiswaDataSource(context: Context) {
  11.     private var database : SQLiteDatabase? = null
  12.     private var helper : DatabaseHelper
  13.  
  14.     init {
  15.         helper = DatabaseHelper(context)
  16.     }
  17.  
  18.     private fun openAccess(){
  19.         database = helper.writableDatabase
  20.     }
  21.  
  22.     private fun closeAccess(){
  23.         database?.close()
  24.         helper.close()
  25.     }
  26.  
  27.     fun insertStudent(siswa: Siswa){
  28.         openAccess()
  29.         var cv = ContentValues()
  30.         cv.put(DatabaseHelper.COL_NAMADEPAN , siswa.namad)
  31.         cv.put("nama_belakang" , siswa.namab)
  32.         cv.put("no_hp" , siswa.noHp)
  33.         cv.put("hobi" , siswa.hobi)
  34.         cv.put("alamat" , siswa.alamat)
  35.         cv.put("gender" , siswa.gender)
  36.         cv.put("jenjang" , siswa.jenjang)
  37.         cv.put("email" , siswa.email)
  38.         cv.put("tgl_lahir" , siswa.tglLahir)
  39.         var database =database
  40.         var long = database?.insertOrThrow("siswa", null
  41.             , cv)
  42.         closeAccess()
  43.     }
  44.  
  45.     private fun fetchRow(cursor: Cursor) : Siswa{
  46.         var siswa  = Siswa()
  47.         siswa.id = cursor.getInt(0)
  48.  
  49.         var index = cursor.getColumnIndex(DatabaseHelper.COL_NAMADEPAN)
  50.         siswa.namad = cursor.getString(index)
  51.  
  52.         var indexNamab = cursor.getColumnIndex("nama_belakang")
  53.         siswa.namab = cursor.getString(indexNamab)
  54.         siswa.noHp = cursor.getString(3)
  55.         siswa.jenjang = cursor.getString(4)
  56.         siswa.hobi = cursor.getString(5)
  57.         siswa.alamat= cursor.getString(6)
  58.         siswa.email = cursor.getString(7)
  59.         siswa.tglLahir= cursor.getString(8)
  60.         siswa.gender= cursor.getString(9)
  61.         return siswa
  62.  
  63.  
  64.  
  65.     }
  66.  
  67.     fun getAllData() : ArrayList<Siswa>{
  68.         openAccess()
  69.         var cursor = database?.rawQuery("" +
  70.                 "SELECT * FROM siswa", null)
  71.         cursor?.moveToFirst()
  72.  
  73.         var listAll = ArrayList<Siswa> ()
  74.  
  75.         while (!cursor!!.isAfterLast){
  76.             var oneRowSiswa = fetchRow(cursor)
  77.             listAll.add(oneRowSiswa)
  78.             cursor.moveToNext()
  79.         }
  80.         cursor.close()
  81.         closeAccess()
  82.         return listAll
  83.  
  84.     }
  85.  
  86.  
  87.  
  88.  
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top