diff --git a/README.md b/README.md index df2a6477..585b9b2f 100644 --- a/README.md +++ b/README.md @@ -1 +1,12 @@ # android-map-keyword +# 1단계 + +## 기능 요구 사항 +- 카카오맵 클론 코딩을 위한 시작입니다. +- 검색어 입력 및 검색 결과를 표시할 기본 레이아웃을 구현한다. +- 검색에 사용될 데이터를 로컬 데이터베이스에 생성한다. + +## 프로그래밍 요구 사항 +- 검색 데이터는 저장은 SQLite를 사용한다. +- 가능한 MVVM 아키텍처 패턴을 적용하도록 한다. +- 코드 컨벤션을 준수하며 프로그래밍한다. 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..5d399caf 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,25 @@ package campus.tech.kakao.map +import android.content.ContentValues import android.os.Bundle +import android.util.Log import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + + val dbHelper = PlaceDbHelper(applicationContext) + val db = dbHelper.writableDatabase +// dbHelper.onUpgrade(db, 1, 2) + val mainViewModel = MainViewModel(application) + mainViewModel.insertInitData() + var places = mutableListOf() + places = dbHelper.searchPlaceName("") + places.forEach { + Log.d("testt", "onCreate: ${it.name}") + } + } } diff --git a/app/src/main/java/campus/tech/kakao/map/MainViewModel.kt b/app/src/main/java/campus/tech/kakao/map/MainViewModel.kt new file mode 100644 index 00000000..242419ed --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/MainViewModel.kt @@ -0,0 +1,21 @@ +package campus.tech.kakao.map + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel + +class MainViewModel(application: Application): AndroidViewModel(application) { + private var placeList = MutableLiveData>() + private val dbHelper = PlaceDbHelper(application) + + fun insertInitData(){ + if (!dbHelper.existData()){ + for(i in 1..10){ + dbHelper.addPlace(Place("카페 $i", "남양주 $i", "카페")) + dbHelper.addPlace(Place("약국 $i", "남양주 $i", "약국")) + } + } + + } +} \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/Place.kt b/app/src/main/java/campus/tech/kakao/map/Place.kt new file mode 100644 index 00000000..904256a6 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/Place.kt @@ -0,0 +1,3 @@ +package campus.tech.kakao.map + +data class Place(val name: String, val address: String, val type: String) diff --git a/app/src/main/java/campus/tech/kakao/map/PlaceContract.kt b/app/src/main/java/campus/tech/kakao/map/PlaceContract.kt new file mode 100644 index 00000000..e8d1aff1 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/PlaceContract.kt @@ -0,0 +1,10 @@ +package campus.tech.kakao.map + +import android.provider.BaseColumns + +object PlaceContract:BaseColumns { + const val TABLE_NAME = "place" + const val COLUMN_NAME_NAME = "name" + const val COLUMN_NAME_ADDRESS = "address" + const val COLUMN_NAME_TYPE = "type" +} \ No newline at end of file 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..4923ba61 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/PlaceDbHelper.kt @@ -0,0 +1,98 @@ +package campus.tech.kakao.map + +import android.content.ContentValues +import android.content.Context +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper + +class PlaceDbHelper(context: Context):SQLiteOpenHelper( + context, "place.db", null, 2) { + override fun onCreate(db: SQLiteDatabase?) { + db?.execSQL("CREATE TABLE ${PlaceContract.TABLE_NAME} " + + "(${PlaceContract.COLUMN_NAME_NAME} TEXT, " + + "${PlaceContract.COLUMN_NAME_ADDRESS} TEXT, " + + "${PlaceContract.COLUMN_NAME_TYPE} TEXT)") + } + + override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { + db?.execSQL("DROP TABLE IF EXISTS ${PlaceContract.TABLE_NAME}") + onCreate(db) + } + + fun addPlace(place: Place) { + val db = writableDatabase + if (!existPlace(place, db)){ + val values = ContentValues() + values.put(PlaceContract.COLUMN_NAME_NAME, place.name) + values.put(PlaceContract.COLUMN_NAME_ADDRESS, place.address) + values.put(PlaceContract.COLUMN_NAME_TYPE, place.type) + db.insert(PlaceContract.TABLE_NAME, null, values) + db.close() + } + } + + fun existData(): Boolean{ + val db = readableDatabase + val cursor = db.query( + PlaceContract.TABLE_NAME, + arrayOf(PlaceContract.COLUMN_NAME_NAME), + null, + null, + null, + null, + null + ) + val result = cursor.moveToFirst() + cursor.close() + return if (result) true else false + } + + fun existPlace(place: Place, db: SQLiteDatabase): Boolean{ + val selection = "${PlaceContract.COLUMN_NAME_NAME} like ? AND " + + "${PlaceContract.COLUMN_NAME_ADDRESS} like ? AND " + + "${PlaceContract.COLUMN_NAME_TYPE} like ?" + val cursor = db.query( + PlaceContract.TABLE_NAME, + arrayOf(PlaceContract.COLUMN_NAME_NAME), + selection, + arrayOf(place.name, place.address, place.type), + null, + null, + "${PlaceContract.COLUMN_NAME_NAME} DESC" + ) + + val result = cursor.moveToFirst() + cursor.close() + return if (result) true else false + } + + fun searchPlaceName(name: String): MutableList{ + val results = mutableListOf() + var searchResult = "%${name}%" + val cursor = readableDatabase.query( + PlaceContract.TABLE_NAME, + arrayOf(PlaceContract.COLUMN_NAME_NAME, + PlaceContract.COLUMN_NAME_ADDRESS, + PlaceContract.COLUMN_NAME_TYPE), + "${PlaceContract.COLUMN_NAME_NAME} like ?", + arrayOf(searchResult), + null, + null, + "${PlaceContract.COLUMN_NAME_NAME} ASC" + ) + + while (cursor.moveToNext()) { + val name = cursor.getString( + cursor.getColumnIndexOrThrow(PlaceContract.COLUMN_NAME_NAME) + ) + val address = cursor.getString( + cursor.getColumnIndexOrThrow(PlaceContract.COLUMN_NAME_ADDRESS) + ) + val type = cursor.getString( + cursor.getColumnIndexOrThrow(PlaceContract.COLUMN_NAME_TYPE)) + results.add(Place(name, address, type)) + } + cursor.close() + return results + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/x.png b/app/src/main/res/drawable/x.png new file mode 100644 index 00000000..15dcc1b3 Binary files /dev/null and b/app/src/main/res/drawable/x.png differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 24d17df2..dcaefe47 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,10 +7,35 @@ android:layout_height="match_parent" tools:context=".MainActivity"> + + + + + + + Map + 검색어를 입력해 주세요. + 검색 결과가 없습니다. \ No newline at end of file