· 8 years ago · Jun 18, 2018, 08:26 AM
1import android.content.Context
2import android.database.Cursor
3import android.database.sqlite.SQLiteDatabase
4import android.database.sqlite.SQLiteOpenHelper
5import android.os.Build
6import android.widget.Toast
7import java.io.FileOutputStream
8import java.io.IOException
9import java.lang.Exception
10import java.util.ArrayList
11
12class DbManager {
13
14 var DB_PATH = ""
15 var DB_NAME = "account.db"
16 val dbVersion = 1
17 //CREATE TABLE IF NOT EXISTS MyNotes (ID INTEGER PRIMARY KEY,title TEXT, Description TEXT);"
18// val sqlCreateTable = "CREATE TABLE IF NOT EXISTS " + dbTable + " (" + colID + " INTEGER PRIMARY KEY," +
19// colTitle + " TEXT, " + colDes + " TEXT);"
20 var sqlDB: SQLiteDatabase? = null
21
22 constructor(context: Context) {
23 var db = DBHelper(context)
24 sqlDB = db.writableDatabase
25 }
26
27 inner class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, dbVersion) {
28 var mDatabase: SQLiteDatabase? = null
29 var mContext: Context? = null
30
31 //Todo: Get All Data users
32 fun getAllUsers(): List<Account>?{
33 val temp = ArrayList<Account>()
34 val db = writableDatabase
35 var c: Cursor?
36
37 try {
38 c = db.rawQuery("SELECT * FROM Account ", null)
39 if (c == null) return null
40 c.moveToFirst()
41
42 do {
43 val account = Account(c.getString(c.getColumnIndex("name")), c.getString(c.getColumnIndex("email")))
44 temp.add(account)
45 } while (c.moveToNext())
46 c.close()
47 } catch (e: Exception) {
48 }
49
50 db.close()
51 return temp
52 }
53
54 fun createDataBase() {
55 //Todo: Create Database
56 val isDBExist = checkDataBase()
57 if (isDBExist) { } else {
58 this.readableDatabase
59 try {
60 copyDataBase()
61 Toast.makeText(this.mContext, "Copy has been finished.", Toast.LENGTH_SHORT).show()
62 } catch (ex: Exception) {
63 }
64 }
65 }
66
67 init {
68
69 if (Build.VERSION.SDK_INT > 17) {
70 DB_PATH = context.applicationInfo.dataDir + "/databases/"
71 } else {
72 DB_PATH = "/data/data/" + context.packageName + "/databases/"
73 }
74
75 this.mContext = mContext
76 }
77
78 override fun close() {
79 if (mDatabase != null)
80 mDatabase!!.close()
81 super.close()
82 }
83
84 fun checkDataBase(): Boolean {
85 //Todo: Check Database
86 var tempDB: SQLiteDatabase? = null
87 try {
88 val path = DB_PATH + DB_NAME
89 tempDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY)
90 } catch (ex: Exception) {
91 }
92
93 if (tempDB != null)
94 tempDB.close()
95 return if (tempDB != null) true else false
96 }
97
98 fun copyDataBase() {
99 //Todo:Copy Database
100
101 try {
102 val myInput = mContext!!.assets.open(DB_NAME)
103 val outputFileName = DB_PATH + DB_NAME
104 val myOutput = FileOutputStream(outputFileName)
105
106 val buffer = ByteArray(1024)
107 var length: Int
108 length= myInput.read(buffer)
109 while (length > 0) {
110 myOutput.write(buffer, 0, length)
111 }
112 myOutput.flush()
113 myOutput.close()
114 myInput.close()
115
116 } catch (e: IOException) {
117 e.printStackTrace()
118 }
119
120 }
121
122 fun openDataBase() {
123 //Todo: Open Database
124 val path = DB_PATH + DB_NAME
125 mDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE)
126 }
127
128 override fun onCreate(db: SQLiteDatabase) {
129 createDataBase()
130 }
131
132 override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
133 if(oldVersion>=newVersion){
134 copyDataBase()
135 }
136 }
137
138// companion object {
139// var DB_PATH = ""
140// var DB_NAME = "account.db"
141// }
142 }
143
144}
145
146import android.content.Context
147import android.support.v7.app.AppCompatActivity
148import android.os.Bundle
149import android.view.LayoutInflater
150import android.view.View
151import android.view.ViewGroup
152import android.widget.BaseAdapter
153import android.widget.Button
154import android.widget.LinearLayout
155import android.widget.TextView
156import kotlinx.android.synthetic.main.activity_main.*
157import kotlinx.android.synthetic.main.row.*
158import kotlinx.android.synthetic.main.row.view.*
159import java.util.ArrayList
160
161class MainActivity : AppCompatActivity() {
162 var lstUsers=ArrayList<Account>()
163 lateinit var dbHelper: DBHelper
164
165 override fun onCreate(savedInstanceState: Bundle?) {
166 super.onCreate(savedInstanceState)
167 setContentView(R.layout.activity_main)
168 dbHelper = DBHelper(applicationContext)
169 dbHelper.createDataBase()
170 }
171
172 fun buGetData(v: View){
173 //Todo: Load Data from db set to listview
174 LoadData()
175 }
176
177
178 fun LoadData(){
179
180 val accout = dbHelper.getAllUsers()
181 //this.listView.removeAllViews()
182 //List View
183 for (list in accout!!) {
184 lstUsers.add(list)
185 }
186
187
188 var myNotesAdapter= MyAdapter(this, lstUsers)
189 listView.adapter=myNotesAdapter
190
191 }
192
193
194 inner class MyAdapter:BaseAdapter {
195
196 var listMyAdapter = ArrayList<Account>()
197 var context:Context?=null
198 constructor(context:Context, listMyAdapter:ArrayList<Account>):super(){
199 this.listMyAdapter=listMyAdapter
200 this.context=context
201 }
202
203 override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
204 var myView=layoutInflater.inflate(R.layout.row,null)
205 var myAc=listMyAdapter[position]
206 myView.txtUser.text = myAc.userName
207 myView.txtEmail.text = myAc.email
208
209 return myView
210 }
211
212 override fun getItem(position: Int): Any {
213 return listMyAdapter[position]
214 }
215
216 override fun getItemId(position: Int): Long {
217 return position.toLong()
218 }
219
220 override fun getCount(): Int {
221 return listMyAdapter.size
222 }
223
224 }
225}