diff --git a/README.md b/README.md index df2a6477..51f63b77 100644 --- a/README.md +++ b/README.md @@ -1 +1,6 @@ # android-map-keyword +## 카카오맵 클론 코딩을 위한 시작 + +### step 1 +- 검색어 입력 및 검색 결과를 표시할 기본 레이아웃을 구현한다. +- 검색에 사용될 데이터를 로컬 데이터베이스에 생성한다. \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt index 95b43803..94872839 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -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) + + try { + db = dbHelper.writableDatabase + } catch (e: Exception) { + e.printStackTrace() + } + + cancelButton.setOnClickListener { + searchEditText.text.clear() + } } } + + diff --git a/app/src/main/java/campus/tech/kakao/map/PlaceDBHelper.kt b/app/src/main/java/campus/tech/kakao/map/PlaceDBHelper.kt new file mode 100644 index 00000000..0a5462b2 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/PlaceDBHelper.kt @@ -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" + +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) + } + + private fun insertInitialRecords(db: SQLiteDatabase?) { + 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" + 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" + } +} diff --git a/app/src/main/res/drawable/cancel.png b/app/src/main/res/drawable/cancel.png new file mode 100644 index 00000000..8f3daaa4 Binary files /dev/null and b/app/src/main/res/drawable/cancel.png differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 24d17df2..a33b0c5d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,19 +1,56 @@ - + + + android:layout_height="wrap_content" + android:layout_marginTop="16dp" + android:layout_marginStart="16dp" + android:layout_marginEnd="16dp"> - + android:layout_alignParentStart="true" + android:layout_toStartOf="@id/cancelButton" + android:layout_weight="1" + android:hint="검색어를 입력해 주세요." + android:padding="8dp" + android:background="@android:drawable/edit_text" /> + + + + + + + + + - + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 768b058a..6ff2d795 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -2,4 +2,5 @@ #FF000000 #FFFFFFFF + #FF808080