[코틀린을 활용한 안드로이드 프로그래밍] 12장 직접 풀어보기 12-2

2022. 6. 9. 01:42·안드로이드 프로그래밍/코틀린
반응형

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

<MainActivity>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.cookandroid.project12_2
 
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    lateinit var myHelper : myDBHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        title = "가수 그룹 관리 DB"
        myHelper = myDBHelper(this)
        btnInit.setOnClickListener {
            var sqlDB = myHelper.writableDatabase
            myHelper.onUpgrade(sqlDB, 1, 2)
            sqlDB.close()
        }
 
        btnInsert.setOnClickListener {
            var sqlDB = myHelper.writableDatabase
            sqlDB.execSQL("INSERT INTO groupTBL VALUES('"+ edtName.text.toString() + "' ,"+edtNumber.text.toString() + " ); "  )
            sqlDB.close()
            Toast.makeText(applicationContext, "입력됨", Toast.LENGTH_SHORT).show()
        }
        btnSelect.setOnClickListener {
            var sqlDB = myHelper.readableDatabase
            var cursor = sqlDB.rawQuery("SELECT * FROM groupTBL;", null)
 
            var strNames = "그룹이름" + "\r\n" + "-----" +"\r\n"
            var strNumbers = "인원" + "\r\n" + "-----" +"\r\n"
 
            while(cursor.moveToNext()){
                strNames += cursor.getString(0) + "\r\n"
                strNumbers += cursor.getString(1) + "\r\n"
            }
 
            tvNameResult.text = strNames
            tvNumberResult.text = strNumbers
 
            cursor.close()
            sqlDB.close()
 
        }
 
        btnUpdate.setOnClickListener {
            var sqlDB = myHelper.writableDatabase
            var afterNumber = edtNumber.text.toString().toInt()
            var targetName = edtName.text.toString()
            sqlDB.execSQL("UPDATE groupTBL SET gNumber = '${afterNumber}' WHERE gName = '${targetName}';")
 
            Toast.makeText(applicationContext, "수정됨", Toast.LENGTH_SHORT).show()
            btnSelect.callOnClick()
        }
 
        btnDelete.setOnClickListener {
            var sqlDB = myHelper.writableDatabase
            var targetName = edtName.text.toString()
            sqlDB.execSQL("DELETE FROM groupTBL WHERE gName = '${targetName}';")
            Toast.makeText(applicationContext, "삭제됨", Toast.LENGTH_SHORT).show()
            btnSelect.callOnClick()
        }
 
    }
 
    inner class myDBHelper(context: Context) :SQLiteOpenHelper(context, "groupDB", null, 1){
        override fun onCreate(db: SQLiteDatabase?) {
            db!!.execSQL("CREATE TABLE groupTBL(gName CHAR(20) PRIMARY KEY, gNumber INTEGER)")
        }
 
        override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
            db!!.execSQL("DROP TABLE IF EXISTS groupTBL")
            onCreate(db)
        }
 
    }
}
Colored by Color Scripter
cs

 

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="이름 : "/>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/edtName"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="인원 : "/>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/edtNumber"/>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
 
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="초기"
            android:id="@+id/btnInit"/>
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="입력"
            android:id="@+id/btnInsert"/>
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="수정"
            android:id="@+id/btnUpdate"/>
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="삭제"
            android:id="@+id/btnDelete"/>
 
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="조회"
            android:id="@+id/btnSelect"/>
 
 
    </LinearLayout>
    <LinearLayout
        android:gravity="center"
        android:background="#00FF00"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/tvNameResult"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/tvNumberResult"/>
    </LinearLayout>
 
</LinearLayout>
Colored by Color Scripter
cs
반응형
저작자표시

'안드로이드 프로그래밍 > 코틀린' 카테고리의 다른 글

[코틀린을 활용한 안드로이드 프로그래밍] 13장 직접 풀어보기 13-2  (0) 2022.06.09
[코틀린을 활용한 안드로이드 프로그래밍] 13장 직접 풀어보기 13-1  (0) 2022.06.09
[코틀린 안드로이드 프로그래밍] Spinner OnItemSelectedListener사용법  (0) 2022.06.08
[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-3  (0) 2022.06.08
[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-2  (0) 2022.06.08
'안드로이드 프로그래밍/코틀린' 카테고리의 다른 글
  • [코틀린을 활용한 안드로이드 프로그래밍] 13장 직접 풀어보기 13-2
  • [코틀린을 활용한 안드로이드 프로그래밍] 13장 직접 풀어보기 13-1
  • [코틀린 안드로이드 프로그래밍] Spinner OnItemSelectedListener사용법
  • [코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-3
슥지니
슥지니
개발 블로그
  • 슥지니
    슥지니의 코딩노트
    슥지니
  • 전체
    오늘
    어제
    • 분류 전체보기 (199)
      • 알고리즘 문제풀이 (158)
        • 백준 (158)
      • 알고리즘 (6)
      • Node.js (2)
        • MongoDB (1)
        • 기타 (1)
      • spring (0)
      • 가상화폐 (1)
        • 바이낸스(Binance) (1)
      • C++ 테트리스 게임 (1)
      • C++ (10)
      • 안드로이드 프로그래밍 (21)
        • 코틀린 (21)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    자료구조
    시뮬레이션
    코틀린
    BFS
    우선순위 큐
    dp
    C++
    콘솔 테트리스 게임
    구현
    알고리즘
    그래프
    백트랙킹
    콘솔
    코틀린을 활용한 안드로이드 프로그래밍
    Kotlin
    백준
    다이나믹 프로그래밍
    그리디
    C
    dfs
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
슥지니
[코틀린을 활용한 안드로이드 프로그래밍] 12장 직접 풀어보기 12-2
상단으로

티스토리툴바