-
Notifications
You must be signed in to change notification settings - Fork 33
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 #25
base: fivejinw
Are you sure you want to change the base?
Changes from all commits
9fb7585
3228d66
9249097
8ee5a1e
1936a12
576db54
25408b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
# android-map-keyword | ||
# 2주차 - android-map-keyword | ||
## step1 구현할 기능 목록 | ||
|
||
**UI 관련 기능** | ||
- [x] 검색어를 입력할 수 있는 칸 구현 | ||
- [x] 검색 결과를 표시할 수 있는 칸 구현 | ||
- [ ] 저장된 검색 결과를 표시할 수 있는 칸 구현 | ||
|
||
**DB 관련 기능** | ||
- [ ] 장소의 이름, 장소의 위치. 장소 카테고리를 저장할 수 있는 데이터베이스 구 |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package campus.tech.kakao.map.db | ||
|
||
import android.provider.BaseColumns | ||
|
||
object PlaceContract{ | ||
object PlaceEntry : BaseColumns { | ||
const val TABLE_NAME = "PLACE" | ||
const val COLUMN_NAME = "NAME" | ||
const val COLUMN_LOCATION = "LOCATION" | ||
const val COLUMN_CATEGORY = "CATEGORY" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package campus.tech.kakao.map.db | ||
|
||
import android.content.ContentValues | ||
import android.content.Context | ||
import android.database.Cursor | ||
import android.database.sqlite.SQLiteDatabase | ||
import android.database.sqlite.SQLiteOpenHelper | ||
|
||
|
||
class PlaceDBHelper(context:Context) : SQLiteOpenHelper(context, "place.db", null, 1){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. place.db 와 같은 것들도 const 로 정의하면 조금 더 좋겠네요! |
||
override fun onCreate(db: SQLiteDatabase?) { | ||
db?.execSQL("CREATE TABLE " + | ||
"${PlaceContract.PlaceEntry.TABLE_NAME}( " + | ||
" ${PlaceContract.PlaceEntry.COLUMN_NAME} varchar(60) not null ," + | ||
" ${PlaceContract.PlaceEntry.COLUMN_LOCATION} varchar(255) not null, " + | ||
" ${PlaceContract.PlaceEntry.COLUMN_CATEGORY} varchar(30) );" | ||
) | ||
} | ||
|
||
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { | ||
db?.execSQL("DROP TABLE IF EXISTS ${PlaceContract.PlaceEntry.TABLE_NAME};") | ||
onCreate(db) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. onCreate 를 직접 호출하기 보다 onCreate 에서 수행하는 로직을 따로 메소드로 분리하는 것은 어떨까요? override fun onCreate(db: SQLiteDatabase) {
createLocationTable(db)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion) {
db?.execSQL("DROP TABLE IF EXISTS ${PlaceContract.PlaceEntry.TABLE_NAME};")
createLocationTable(db)
}
private fun createLocationTable(db: SQLiteDatabase) {
db?.execSQL("CREATE TABLE " +
"${PlaceContract.PlaceEntry.TABLE_NAME}( " +
" ${PlaceContract.PlaceEntry.COLUMN_NAME} varchar(60) not null ," +
" ${PlaceContract.PlaceEntry.COLUMN_LOCATION} varchar(255) not null, " +
" ${PlaceContract.PlaceEntry.COLUMN_CATEGORY} varchar(30) );"
)
} |
||
} | ||
|
||
fun insertDummyData(){ | ||
val name = "카페" | ||
val category = "카페" | ||
val location = "서울 성동구 성수동" | ||
for(i in 1..10){ | ||
insertData(name + i, location + i, category) | ||
} | ||
readData() | ||
} | ||
Comment on lines
+25
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용하고 있지 않은 코드네요! |
||
|
||
fun insertData(name: String, location: String, category: String) { | ||
val db = this.writableDatabase | ||
val values = ContentValues().apply { | ||
put(PlaceContract.PlaceEntry.COLUMN_NAME, name) | ||
put(PlaceContract.PlaceEntry.COLUMN_LOCATION, location) | ||
put(PlaceContract.PlaceEntry.COLUMN_CATEGORY, category) | ||
} | ||
db.insert(PlaceContract.PlaceEntry.TABLE_NAME, null, values) | ||
} | ||
|
||
fun readData(): Cursor { | ||
val rDb = this.readableDatabase | ||
|
||
return rDb.rawQuery("SELECT * FROM ${PlaceContract.PlaceEntry.TABLE_NAME};", null) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package campus.tech.kakao.map.model | ||
|
||
data class Place( | ||
val name : String, | ||
val location : String, | ||
val category : String | ||
) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,36 @@ | ||||||
package campus.tech.kakao.map.repository | ||||||
|
||||||
import android.util.Log | ||||||
import campus.tech.kakao.map.db.PlaceContract | ||||||
import campus.tech.kakao.map.db.PlaceDBHelper | ||||||
import campus.tech.kakao.map.model.Place | ||||||
|
||||||
class Repository (val dbHelper: PlaceDBHelper){ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Repository 의 네이밍이 조금 더 명확했으면 좋겠습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 또한 Repository 가 PlaceDBHelper 를 가지고 있는데, PlaceDBHelper 의 close 를 호출하는 부분이 없네요! 앱 사용에 따라 반드시 close 되어야 합니다. 앱 라이프사이클에 따라 close 시점을 고민해보시면 좋을 것 같습니다! |
||||||
fun getAllPlace() : MutableList<Place>{ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getAllPlace 는 수정될 여지가 없기 때문에 MutableList 를 반환하기 보다는 Immutable 한 List 를 반환하는 것이 어떨까요?
Suggested change
|
||||||
val cursor = dbHelper.readData() | ||||||
val result = mutableListOf<Place>() | ||||||
|
||||||
while (cursor.moveToNext()) { | ||||||
val place = Place( | ||||||
cursor.getString( | ||||||
cursor.getColumnIndexOrThrow(PlaceContract.PlaceEntry.COLUMN_NAME) | ||||||
), | ||||||
cursor.getString( | ||||||
cursor.getColumnIndexOrThrow(PlaceContract.PlaceEntry.COLUMN_LOCATION) | ||||||
), | ||||||
cursor.getString( | ||||||
cursor.getColumnIndexOrThrow(PlaceContract.PlaceEntry.COLUMN_CATEGORY) | ||||||
) | ||||||
) | ||||||
Log.d("readData", "이름 = ${place.name}, 위치 = ${place.location}, 분류 = ${place.category}") | ||||||
result.add(place) | ||||||
} | ||||||
|
||||||
cursor.close() | ||||||
return result | ||||||
} | ||||||
|
||||||
fun writePlace(place: Place){ | ||||||
dbHelper.insertData(place.name, place.location, place.category) | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package campus.tech.kakao.map.view | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.ViewModelProvider | ||
import campus.tech.kakao.map.db.PlaceDBHelper | ||
import campus.tech.kakao.map.R | ||
import campus.tech.kakao.map.repository.Repository | ||
import campus.tech.kakao.map.viewmodel.MainActivityViewModel | ||
import campus.tech.kakao.map.viewmodel.ViewModelFactory | ||
|
||
class MainActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
val dbHelper = PlaceDBHelper(this) | ||
val repository = Repository(dbHelper) | ||
val viewModel = ViewModelProvider(this, ViewModelFactory(repository))[MainActivityViewModel::class.java] | ||
viewModel.getPlace() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package campus.tech.kakao.map.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import campus.tech.kakao.map.repository.Repository | ||
|
||
class MainActivityViewModel (private val repository: Repository) : ViewModel() { | ||
fun getPlace(){ | ||
repository.getAllPlace() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package campus.tech.kakao.map.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import campus.tech.kakao.map.repository.Repository | ||
|
||
class ViewModelFactory(private val repository: Repository) : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if(modelClass.isAssignableFrom(MainActivityViewModel::class.java)){ | ||
return MainActivityViewModel(repository) as T | ||
} | ||
throw IllegalArgumentException("unKnown ViewModel class") | ||
} | ||
} |
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍