Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

경북대 Android_남지연 2주차 과제_step1 #27

Open
wants to merge 5 commits into
base: njiyeon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# android-map-keyword
## 카카오맵 클론 코딩을 위한 시작

### step 1
- 검색어 입력 및 검색 결과를 표시할 기본 레이아웃을 구현한다.
- 검색에 사용될 데이터를 로컬 데이터베이스에 생성한다.
33 changes: 33 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
package campus.tech.kakao.map

import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.widget.EditText
import android.widget.ImageButton
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

private lateinit var searchEditText: EditText
private lateinit var cancelButton: ImageButton
private lateinit var noResultsTextView: TextView
private lateinit var resultsListView: ListView

private lateinit var dbHelper: PlaceDBHelper
private lateinit var db: SQLiteDatabase

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

searchEditText = findViewById(R.id.searchEditText)
cancelButton = findViewById(R.id.cancelButton)
noResultsTextView = findViewById(R.id.noResultsTextView)
resultsListView = findViewById(R.id.resultsListView)

dbHelper = PlaceDBHelper(this)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dbHelper를 onDestroy 콜백에서 닫아주세요!


try {
db = dbHelper.writableDatabase
} catch (e: Exception) {
e.printStackTrace()
}

cancelButton.setOnClickListener {
searchEditText.text.clear()
}
}
}


73 changes: 73 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/PlaceDBHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package campus.tech.kakao.map

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

private const val TABLE_NAME = "places"
private const val COLUMN_CATEGORY = "category"
private const val COLUMN_TITLE = "title"
private const val COLUMN_LOCATION = "location"
Comment on lines +7 to +10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PlcaeContract 등으로 따로 클래스 만들어서 빼는게 좋겠네요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 step2에 위와 같이 수정하여 제출하였습니다 !


class PlaceDBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {

override fun onCreate(db: SQLiteDatabase?) {
db?.execSQL(SQL_CREATE_TABLE)
insertInitialRecords(db)
}

override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
db?.execSQL(SQL_DROP_TABLE)
onCreate(db)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on~~ 으로 붙은 함수들은 일반적으로 콜백 메서드로 사용됩니다. 내부 동작에 영향을 미치는 동작일 수 있기 때문에 Drop 후 create 하시려는거면 따로 함수를 빼서 작성하시는게 좋습니다.

}

private fun insertInitialRecords(db: SQLiteDatabase?) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

잘 사용하셨습니다! 👍

db?.beginTransaction()
try {
db?.let {
insertCafeRecords(it)
insertPharmacyRecords(it)
}
db?.setTransactionSuccessful()
} finally {
db?.endTransaction()
}
}

private fun insertCafeRecords(db: SQLiteDatabase) {
val category = "카페"
val titleBase = "카페"
val locationBase = "서울 성동구 성수동"

for (i in 1..20) {
val title = "$titleBase$i"
val location = "$locationBase $i"
db.execSQL(
"INSERT INTO $TABLE_NAME ($COLUMN_CATEGORY, $COLUMN_TITLE, $COLUMN_LOCATION) VALUES ('$category', '$title', '$location');"
)
}
}
private fun insertPharmacyRecords(db: SQLiteDatabase) {
val category = "약국"
val titleBase = "약국"
val locationBase = "서울 강남구 대치동"

for (i in 1..20) {
val title = "$titleBase$i"
val location = "$locationBase $i"
db.execSQL(
"INSERT INTO $TABLE_NAME ($COLUMN_CATEGORY, $COLUMN_TITLE, $COLUMN_LOCATION) VALUES ('$category', '$title', '$location');"
)
}
}
companion object {
private const val DB_VERSION = 1
private const val DB_NAME = "location.db"
Comment on lines +64 to +65
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DB 관련 정보도 따로 빠지는게 좋겠네요. 여기서는 Place 관련 내용만 작성해서 괜찮지만 나중에 여러 테이블이 나오면 다른 테이블 관련 DBHelper에서 참조해야 한다면 의존성이 생기기에 따로 빼주세요.

private const val SQL_CREATE_TABLE =
"CREATE TABLE $TABLE_NAME (" +
"$COLUMN_CATEGORY TEXT NOT NULL, " +
"$COLUMN_TITLE TEXT NOT NULL, " +
"$COLUMN_LOCATION TEXT NOT NULL);"
private const val SQL_DROP_TABLE = "DROP TABLE IF EXISTS $TABLE_NAME"
}
}
Binary file added app/src/main/res/drawable/cancel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 51 additions & 14 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp">

<TextView
android:layout_width="wrap_content"
<EditText
android:id="@+id/searchEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:layout_alignParentStart="true"
android:layout_toStartOf="@id/cancelButton"
android:layout_weight="1"
android:hint="검색어를 입력해 주세요."
android:padding="8dp"
android:background="@android:drawable/edit_text" />

<ImageButton
android:id="@+id/cancelButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="?android:attr/selectableItemBackground"
android:padding="8dp"
android:scaleType="centerInside"
android:src="@drawable/cancel" />

</RelativeLayout>

<TextView
android:id="@+id/noResultsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="검색 결과가 없습니다."
android:textSize="16sp"
android:textColor="@color/grey"
android:gravity="center"/>

<!-- 이후 2단계에서 RecyclerView로 수정할 것임 -->
<ListView
android:id="@+id/resultsListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="grey">#FF808080</color>
</resources>