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주차 과제 1단계 #63

Open
wants to merge 9 commits into
base: joominchul
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# android-map-keyword
# 1단계

## 기능 요구 사항
- 카카오맵 클론 코딩을 위한 시작입니다.
- 검색어 입력 및 검색 결과를 표시할 기본 레이아웃을 구현한다.
- 검색에 사용될 데이터를 로컬 데이터베이스에 생성한다.

## 프로그래밍 요구 사항
- 검색 데이터는 저장은 SQLite를 사용한다.
- 가능한 MVVM 아키텍처 패턴을 적용하도록 한다.
- 코드 컨벤션을 준수하며 프로그래밍한다.
14 changes: 14 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,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<Place>()
places = dbHelper.searchPlaceName("")
places.forEach {
Log.d("testt", "onCreate: ${it.name}")
}

}
}
21 changes: 21 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -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<List<Place>>()
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", "약국"))
}
}

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

data class Place(val name: String, val address: String, val type: String)
10 changes: 10 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/PlaceContract.kt
Original file line number Diff line number Diff line change
@@ -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"
}
98 changes: 98 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,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<Place>{
val results = mutableListOf<Place>()
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
}
}
Binary file added app/src/main/res/drawable/x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 26 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,35 @@
android:layout_height="match_parent"
tools:context=".MainActivity">



<EditText
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:hint="@string/search_hint"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/search_clear"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/x"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="15dp"
app:layout_constraintBottom_toBottomOf="@+id/search"
android:layout_marginBottom="15dp"/>



<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="@string/no_search_result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">Map</string>
<string name="search_hint">검색어를 입력해 주세요.</string>
<string name="no_search_result">검색 결과가 없습니다.</string>
</resources>