๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Study/Android

[์•ˆ๋“œ๋กœ์ด๋“œ ์ฝ”ํ‹€๋ฆฐ] 14. DB ํ”„๋กœ์ ํŠธ - ์ˆ˜๊ฐ•์ƒ ์ •๋ณด ๊ด€๋ฆฌ

DB ํ”„๋กœ์ ํŠธ

 

์ˆ˜๊ฐ•์ƒ ์ •๋ณด ๊ด€๋ฆฌ

์ฃผ์š” ๊ตฌ์„ฑ

- ์ด 4๊ฐœ์˜ ํ™”๋ฉด์œผ๋กœ ๊ตฌ์„ฑ๋จ

    - ์•ฑ์„ ์†Œ๊ฐœํ•˜๋Š” ๋ฉ”์ธ ํ™”๋ฉด 

    - ์ˆ˜๊ฐ•์ƒ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋Š” ํ™”๋ฉด

    - ๊ฐœ๋ณ„ ์ˆ˜๊ฐ•์ƒ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ™”๋ฉด

    - ๋ชจ๋“  ์ˆ˜๊ฐ•์ƒ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ™”๋ฉด

- SQLite ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉ

    - ์ถœ๋ ฅํ•ด์•ผ ํ•  ๋ฐ์ดํ„ฐ ์ˆ˜๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š์œผ๋ฏ€๋กœ ๋™์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์„ค์ •ํ•จ

 

ํŒŒ์ผ ํŽธ์ง‘ ์ˆœ์„œ

- ๋ฉ”์ธ์•กํ‹ฐ๋น„ํ‹ฐ

    - MainActivity.kt / activity_main.xml / menu_main.xml

- ์ˆ˜๊ฐ•์ƒ ๋“ฑ๋ก ์•กํ‹ฐ๋น„ํ‹ฐ

    - PersonnelReg.kt / activity_personnel_reg.xml/menu_personnel_reg.xml

- ์ˆ˜๊ฐ•์ƒ ์ •๋ณด ์•กํ‹ฐ๋น„ํ‹ฐ

    - PersonnelInfo.kt / activity_personnel_info.xml / menu_personnel_info.xml 

- ์ˆ˜๊ฐ•์ƒ ๋ชฉ๋ก ์•กํ‹ฐ๋น„ํ‹ฐ

    - PersonnelList.kt / activity_personnel_list.xml / menu_personnel_list.xml

 

1. ๋ฉ”์ธ ํ™”๋ฉด ์ž‘์„ฑํ•˜๊ธฐ

๋ฉ”๋‰ด ๋ฆฌ์†Œ์Šค ํŒŒ์ผ ์ƒ์„ฑ

- ํ”„๋กœ์ ํŠธ ์ฐฝ์—์„œ res์—์„œ ์šฐํด๋ฆญ ๋˜๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค ์ƒ๋‹จ ๋ฉ”๋‰ด์—์„œ File → New → Android Resource Directory ๋ฅผ ํด๋ฆญ

- res ํด๋” ์•„๋ž˜์— menu ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์„ ํƒํ•˜๊ณ  ์šฐํด๋ฆญ ๋˜๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค ์ƒ๋‹จ ๋ฉ”๋‰ด์—์„œ File → New → Menu resource file์„ ํด๋ฆญํ•จ

 

์•กํ‹ฐ๋น„ํ‹ฐ ์ถ”๊ฐ€ํ•˜๊ธฐ

new > Activity > Empty Views Activity

- PersonnelInfo, PersonnelList, PersonnelReg ์ƒ์„ฑ

 

๋ฉ”์ธํ™”๋ฉด

 

์˜ต์…˜๋ฉ”๋‰ด

- ์ˆ˜๊ฐ•์ƒ ๋ชฉ๋ก๊ณผ ์ˆ˜๊ฐ•์ƒ ๋“ฑ๋ก์œผ๋กœ ๊ตฌ์„ฑ

 

๋ฉ”์ธ๋ฉ”๋‰ด ํŽธ์ง‘

- ํ…์ŠคํŠธ ๋ฆฌ์†Œ์Šค ํŽธ์ง‘

    - ๋ฉ”๋‰ด์—์„œ ์‚ฌ์šฉ๋  ์ด๋ฆ„ ์ถ”๊ฐ€ – strings.xml

<resources>
    <string name="app_name">StudentManagement</string>
    <string name="action_home"> ํ™ˆ </string>
    <string name="action_list"> ์ˆ˜๊ฐ•์ƒ ๋ชฉ๋ก </string>
    <string name="action_reg"> ์ˆ˜๊ฐ•์ƒ ๋“ฑ๋ก </string>
    <string name="action_remove"> ์‚ญ์ œ </string>
</resources>

 

์•กํ‹ฐ๋น„ํ‹ฐ

- ์˜ต์…˜๋ฉ”๋‰ด

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {    // ๋ฉ”๋‰ด
        menuInflater.inflate(R.menu.menu_main, menu)            // ์ธํ”Œ๋ ˆ์ดํŠธ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋‰ด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {   // ๋ฉ”๋‰ด ๋ˆŒ๋Ÿฌ ํ™”๋ฉด ์ „ํ™˜(์ธํ…ํŠธ ์‚ฌ์šฉ)
        when(item?.itemId) {
            R.id.action_list -> {
                val intent = Intent(this, PersonnelList::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
            R.id.action_reg -> {
                val intent = Intent(this, PersonnelReg::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
        }

        return super.onOptionsItemSelected(item)
    }
}

 

2. ์ˆ˜๊ฐ•์ƒ ๋“ฑ๋กํ•˜๊ธฐ

์ˆ˜๊ฐ•์ƒ ์ •๋ณด ๋“ฑ๋ก

- ์„ฑ๋ช…, ์„ฑ๋ณ„, ๋‚˜์ด, ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ

- ์ž…๋ ฅํ•œ ๋‚ด์šฉ์ด Personnel ํ…Œ์ด๋ธ”์— ์ €์žฅ

- ๊ฐœ๋ณ„ ์ˆ˜๊ฐ•์ƒ ์ •๋ณด ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ํ˜ธ์ถœ

 

๋ฉ”๋‰ด ๊ตฌ์„ฑ

- res > menu> New Resource File ์—์„œ menu_personnel_reg.xml ์ƒ์„ฑ

- ํ™ˆ, ์ˆ˜๊ฐ•์ƒ ๋ชฉ๋ก

 

ํ™”๋ฉด ์ž‘์„ฑํ•˜๊ธฐ

- action_personnel_reg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="100dp"
    android:paddingRight="30dp"
    android:paddingLeft="30dp"
    tools:context=".PersonnelReg">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="์„ฑ๋ช…" />
        <EditText
            android:id="@+id/edtName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="์„ฑ๋ณ„" />
        <RadioGroup
            android:id="@+id/gender"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="male" />
            <RadioButton
                android:id="@+id/female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="female" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="๋‚˜์ด" />
        <EditText
            android:id="@+id/edtAge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="์ „ํ™”๋ฒˆํ˜ธ" />
        <EditText
            android:id="@+id/edtTel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btnRegister"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="๋“ฑ๋ก" />
    </LinearLayout>
</LinearLayout>

 

DB ์‚ฌ์šฉํ•˜๊ธฐ 

- new > Kotlin File > DBManager ๋ผ๋Š” ์ด๋ฆ„์˜ class ํŒŒ์ผ ์ƒ์„ฑ

- SQLiteOpenHelper ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ DBManager ํด๋ž˜์Šค ์ƒ์„ฑํ•˜๊ธฐ

- onCreate(), onUpgrade() ๋ฉ”์†Œ๋“œ

๋”๋ณด๊ธฐ

โ€ป ํด๋ž˜์Šค ์ƒ์† ๋ฐ›์„ ๋•Œ ์˜ค๋ฅ˜

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {    // ๋ฉ”๋‰ด
        menuInflater.inflate(R.menu.menu_main, menu)            // ์ธํ”Œ๋ ˆ์ดํŠธ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋‰ด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {   // ๋ฉ”๋‰ด ๋ˆŒ๋Ÿฌ ํ™”๋ฉด ์ „ํ™˜(์ธํ…ํŠธ ์‚ฌ์šฉ)
        when(item?.itemId) {
            R.id.action_list -> {
                val intent = Intent(this, PersonnelList::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
            R.id.action_reg -> {
                val intent = Intent(this, PersonnelReg::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
        }

        return super.onOptionsItemSelected(item)
    }
}

 

์ˆ˜๊ฐ•์ƒ ๋“ฑ๋ก ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ 

- PersonnelReg.kt

- ์œ„์ ฏ ๋ณ€์ˆ˜ ๋“ฑ๋กํ•˜๊ธฐ

- ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ๋ฆฌ์Šค๋„ˆ ๊ตฌํ˜„

    - ์ˆ˜๊ฐ•์ƒ ์ •๋ณด DB์— ์ €์žฅ

์˜ต์…˜ ๋ฉ”๋‰ด ์ถ”๊ฐ€ํ•˜๊ธฐ

class PersonnelReg : AppCompatActivity() {

    lateinit var dbManager: DBManager
    lateinit var  sqlitedb: SQLiteDatabase

    lateinit var btnResister: Button
    lateinit var edtName: EditText
    lateinit var edtAge: EditText
    lateinit var edtTel: EditText
    lateinit var rb_gender: RadioGroup
    lateinit var rb_gender_m: RadioButton
    lateinit var rb_gender_f: RadioButton

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_personnel_reg)

        btnResister = findViewById(R.id.btnRegister)
        edtName = findViewById(R.id.edtName)
        edtAge = findViewById(R.id.edtAge)
        edtTel = findViewById(R.id.edtTel)
        rb_gender = findViewById(R.id.gender)
        rb_gender_m = findViewById(R.id.male)
        rb_gender_f = findViewById(R.id.female)

        dbManager = DBManager(this, "personnelDB", null, 1) // DBManager ๊ฐ์ฒด ๋ฐ›์•„์˜ค๊ธฐ

        btnResister.setOnClickListener {
            val str_name: String = edtName.text.toString()
            var str_age: String = edtAge.text.toString()
            val str_tel: String = edtTel.text.toString()

            var str_gender: String = ""
            if(rb_gender.checkedRadioButtonId == R.id.male) {
                str_gender = rb_gender_m.text.toString()
            }
            if(rb_gender.checkedRadioButtonId == R.id.female) {
                str_gender = rb_gender_f.text.toString()
            }

            sqlitedb = dbManager.writableDatabase    // ์“ฐ๊ธฐ ๊ฐ€๋Šฅํ•œ ๋ฒ„์ „์œผ๋กœ DB ๊ฐ€์ ธ์˜ค๊ธฐ
            sqlitedb.execSQL("INSERT INTO personnel VALUES ('" + str_name +"', '" + str_gender +"', " + str_age +", '" + str_tel +"')")
            sqlitedb.close()

            val intent = Intent(this, PersonnelInfo::class.java)
            intent.putExtra("intent_name", str_name)
            startActivity(intent)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {    // ๋ฉ”๋‰ด
        menuInflater.inflate(R.menu.menu_personnel_reg, menu)            // ์ธํ”Œ๋ ˆ์ดํŠธ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋‰ด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {   // ๋ฉ”๋‰ด ๋ˆŒ๋Ÿฌ ํ™”๋ฉด ์ „ํ™˜(์ธํ…ํŠธ ์‚ฌ์šฉ)
        when(item?.itemId) {
            R.id.action_home -> {
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
            R.id.action_list -> {
                val intent = Intent(this, PersonnelList::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
        }

        return super.onOptionsItemSelected(item)
    }
}

 

3. ๊ฐœ๋ณ„ ์ˆ˜๊ฐ•์ƒ ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ

๊ฐœ๋ณ„ ์ˆ˜๊ฐ•์ƒ ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ

- ํŠน์ • ์ธ๋ฌผ์˜ ์„ฑ๋ช…์„ ์ถ”์ถœํ•˜๊ณ , DB ํ…Œ์ด๋ธ”์—์„œ ํ•ด๋‹น ์ธ๋ฌผ์„ ์ถ”์ถœํ•ด์„œ ์ถœ๋ ฅํ•จ

- ์ˆ˜๊ฐ•์ƒ ๋“ฑ๋ก ๋˜๋Š” ๋ชจ๋“  ์ˆ˜๊ฐ•์ƒ ์ •๋ณด์—์„œ ์„ ํƒ๋จ

- ์‚ญ์ œ ๋ฉ”๋‰ด ํด๋ฆญ ์‹œ ํ•ด๋‹น ์ธ๋ฌผ ์ •๋ณด๋ฅผ ํ…Œ์ด๋ธ”์—์„œ ์‚ญ์ œํ•˜๊ณ  ์ „์ฒด ์ˆ˜๊ฐ•์ƒ ๋ชฉ๋ก์œผ๋กœ ์ด๋™ํ•จ

 

๋ฉ”๋‰ด ๊ตฌ์„ฑ

- menu_personnel_info.xml

- ํ™ˆ, ์ˆ˜๊ฐ•์ƒ ๋ชฉ๋ก, ์ˆ˜๊ฐ•์ƒ ๋“ฑ๋ก, ์‚ญ์ œ

 

ํ™”๋ฉด ์ž‘์„ฑํ•˜๊ธฐ

- activity_personnel_info.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="100dp"
    android:paddingRight="16dp"
    android:paddingLeft="16dp"
    tools:context=".PersonnelInfo">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="์„ฑ๋ช…" />
        <TextView
            android:id="@+id/edtName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="์„ฑ๋ณ„" />
        <TextView
            android:id="@+id/gender"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="๋‚˜์ด" />
        <TextView
            android:id="@+id/edtAge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="์ „ํ™”๋ฒˆํ˜ธ" />
        <TextView
            android:id="@+id/edtTel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

 

๊ฐœ๋ณ„ ์ˆ˜๊ฐ•์ƒ ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ

- Personnel_info.kt

๋”๋ณด๊ธฐ

โ€ป value must be ≥ 0 but getcolumnindex can be ์— ๋Œ€ํ•œ ์˜ค๋ฅ˜ ํ•ด๊ฒฐ

→ getColumnIndex ๋Œ€์‹  getColumnIndexOrThrow๋กœ ์ž‘์„ฑ

if(cursor.moveToNext()) {
    str_gender = cursor.getString(cursor.getColumnIndexOrThrow("gender")).toString()     // column ๊ฐ’์œผ๋กœ ์ธ๋ฑ์Šค ์•Œ์•„์˜ค๊ธฐ
    age = cursor.getInt(cursor.getColumnIndexOrThrow("age"))
    str_tel = cursor.getString(cursor.getColumnIndexOrThrow("tel")).toString()
}

 

https://stackoverflow.com/questions/69053061/android-studio-value-must-be-%E2%89%A5-0

class PersonnelInfo : AppCompatActivity() {

    lateinit var dbManager: DBManager
    lateinit var sqlitedb: SQLiteDatabase

    lateinit var tvName: TextView
    lateinit var tvGender: TextView
    lateinit var tvAge: TextView
    lateinit var tvTel: TextView

    lateinit var str_name: String
    lateinit var str_gender: String
    var age: Int = 0
    lateinit var str_tel: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_personnel_info)

        tvName = findViewById(R.id.edtName)
        tvGender = findViewById(R.id.gender)
        tvAge = findViewById(R.id.edtAge)
        tvTel = findViewById(R.id.edtTel)

        val intent = intent
        str_name = intent.getStringExtra("intent_name").toString()

        dbManager = DBManager(this, "personnelDB", null, 1) // DBManager ๊ฐ์ฒด ๋ฐ›์•„์˜ค๊ธฐ
        sqlitedb = dbManager.readableDatabase

        var cursor: Cursor
        cursor = sqlitedb.rawQuery("SELECT * FROM personnel WHERE name = '"+str_name+"';", null)

        if(cursor.moveToNext()) {
            str_gender = cursor.getString(cursor.getColumnIndexOrThrow("gender")).toString()     // column ๊ฐ’์œผ๋กœ ์ธ๋ฑ์Šค ์•Œ์•„์˜ค๊ธฐ
            age = cursor.getInt(cursor.getColumnIndexOrThrow("age"))
            str_tel = cursor.getString(cursor.getColumnIndexOrThrow("tel")).toString()
        }

        cursor.close()
        sqlitedb.close()
        dbManager.close()

        tvName.text = str_name
        tvGender.text = str_gender
        tvAge.text = "" + age
        tvTel.text = str_tel + "\n"
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {    // ๋ฉ”๋‰ด
        menuInflater.inflate(R.menu.menu_personnel_info, menu)            // ์ธํ”Œ๋ ˆ์ดํŠธ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋‰ด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {   // ๋ฉ”๋‰ด ๋ˆŒ๋Ÿฌ ํ™”๋ฉด ์ „ํ™˜(์ธํ…ํŠธ ์‚ฌ์šฉ)
        when(item?.itemId) {
            R.id.action_home -> {
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
            R.id.action_list -> {
                val intent = Intent(this, PersonnelList::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
            R.id.action_reg -> {
                val intent = Intent(this, PersonnelReg::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
            R.id.action_remove -> {

                dbManager = DBManager(this, "personnelDB", null, 1) // DBManager ๊ฐ์ฒด ๋ฐ›์•„์˜ค๊ธฐ
                sqlitedb = dbManager.readableDatabase

                sqlitedb.execSQL("DELETE FROM personnel WHERE name = '"+str_name+"';")
                sqlitedb.close()
                dbManager.close()

                val intent = Intent(this, PersonnelList::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
        }

        return super.onOptionsItemSelected(item)
    }
}

 

4. ๋ชจ๋“  ์ˆ˜๊ฐ•์ƒ ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ

๋ชจ๋“  ์ˆ˜๊ฐ•์ƒ ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ

- DB ํ…Œ์ด๋ธ”์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ์ธ๋ฌผ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜์—ฌ ํ•ญ๋ชฉ๋“ค์„ ์ถœ๋ ฅํ•จ

 

๋ฉ”๋‰ด ๊ตฌ์„ฑ

- menu_personnel_list.xml 

- ํ™ˆ, ์ˆ˜๊ฐ•์ƒ ๋“ฑ๋ก

 

ํ™”๋ฉด ์ž‘์„ฑํ•˜๊ธฐ

- activity_personnel_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".PersonnelList">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/personnel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>
</LinearLayout>

 

๋ชจ๋“  ์ˆ˜๊ฐ•์ƒ ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ

- Personnel_list.kt

class PersonnelList : AppCompatActivity() {

    lateinit var dbManager: DBManager
    lateinit var sqlitedb: SQLiteDatabase
    lateinit var layout: LinearLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_personnel_list)

        dbManager = DBManager(this, "personnelDB", null, 1)
        sqlitedb = dbManager.readableDatabase

        layout = findViewById(R.id.personnel)

        var cursor: Cursor
        cursor = sqlitedb.rawQuery("SELECT * FROM personnel", null)

        var num: Int = 0
        while (cursor.moveToNext()) {

            var str_name = cursor.getString(cursor.getColumnIndexOrThrow("name")).toString()     // column ๊ฐ’์œผ๋กœ ์ธ๋ฑ์Šค ์•Œ์•„์˜ค๊ธฐ
            var str_gender = cursor.getString(cursor.getColumnIndexOrThrow("gender")).toString()
            var age = cursor.getInt(cursor.getColumnIndexOrThrow("age"))
            var str_tel = cursor.getString(cursor.getColumnIndexOrThrow("tel")).toString()

            var layout_item: LinearLayout = LinearLayout(this)
            layout_item.orientation = LinearLayout.VERTICAL
            layout_item.id = num

            var tvName: TextView = TextView(this)
            tvName.text = str_name
            tvName.textSize = 30f
            tvName.setBackgroundColor(Color.LTGRAY)
            layout_item.addView(tvName)

            var tvGender: TextView = TextView(this)
            tvGender.text = str_gender
            layout_item.addView(tvGender)

            var tvAge: TextView = TextView(this)
            tvAge.text = age.toString()
            layout_item.addView(tvAge)

            var tvTel: TextView = TextView(this)
            tvTel.text = str_tel
            layout_item.addView(tvTel)

            layout_item.setOnClickListener {
                val intent = Intent(this, PersonnelInfo::class.java)
                intent.putExtra("intent_name", str_name)
                startActivity(intent)
            }

            layout.addView(layout_item)
            num++;
        }

        cursor.close()
        sqlitedb.close()
        dbManager.close()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {    // ๋ฉ”๋‰ด
        menuInflater.inflate(R.menu.menu_personnel_list, menu)            // ์ธํ”Œ๋ ˆ์ดํŠธ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋‰ด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {   // ๋ฉ”๋‰ด ๋ˆŒ๋Ÿฌ ํ™”๋ฉด ์ „ํ™˜(์ธํ…ํŠธ ์‚ฌ์šฉ)
        when(item?.itemId) {
            R.id.action_home -> {
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
            R.id.action_reg -> {
                val intent = Intent(this, PersonnelReg::class.java)
                startActivity(intent)
                return true     // ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜
            }
        }

        return super.onOptionsItemSelected(item)
    }
}

 

์ตœ์ข