From 439780c3d627506cce54ac5ddb1f28881eebfe74 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Mon, 1 Jul 2024 14:03:22 +0900 Subject: [PATCH 01/23] docs : updated README.md --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index df2a6477..482e7a6e 100644 --- a/README.md +++ b/README.md @@ -1 +1,15 @@ # android-map-keyword + +# 1단계 - 로컬 데이터 + +****** + +## 카카오맵 클론 코딩을 위한 시작입니다. + +****** + +## 기능 요구 사항 + +### 검색어 입력 및 검색 결과를 표시할 기본 레이아웃을 구현 +### 검색에 사용될 데이터를 로컬 데이터베이스에 생성 + From 502739c68a05250ed3963633ce84b0b3fe1c4fb7 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Mon, 1 Jul 2024 14:28:34 +0900 Subject: [PATCH 02/23] feat : Add SearchView --- app/src/main/res/layout/activity_main.xml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 24d17df2..cf998388 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,12 +5,22 @@ android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MainActivity"> + tools:context=".MainActivity" + android:orientation="vertical"> + + + Date: Mon, 1 Jul 2024 14:29:28 +0900 Subject: [PATCH 03/23] feat : Created SQLiteOpenHelper.kt --- app/src/main/java/campus/tech/kakao/map/SQLiteOpenHelper.kt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 app/src/main/java/campus/tech/kakao/map/SQLiteOpenHelper.kt diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteOpenHelper.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteOpenHelper.kt new file mode 100644 index 00000000..f9d0a007 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteOpenHelper.kt @@ -0,0 +1,4 @@ +package campus.tech.kakao.map + +class SQLiteOpenHelper { +} \ No newline at end of file From 670e00a75b1465400a78c449e4b69afe2e717dd1 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Mon, 1 Jul 2024 15:08:36 +0900 Subject: [PATCH 04/23] feat : Created SQLiteDb.kt --- .../campus/tech/kakao/map/{SQLiteOpenHelper.kt => SQLiteDb.kt} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename app/src/main/java/campus/tech/kakao/map/{SQLiteOpenHelper.kt => SQLiteDb.kt} (56%) diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteOpenHelper.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt similarity index 56% rename from app/src/main/java/campus/tech/kakao/map/SQLiteOpenHelper.kt rename to app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt index f9d0a007..bb59bce4 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteOpenHelper.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt @@ -1,4 +1,4 @@ package campus.tech.kakao.map -class SQLiteOpenHelper { +class SQLiteDb { } \ No newline at end of file From c01c1b6aaaef1062f1279d33cd2422cc63997977 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Mon, 1 Jul 2024 16:12:55 +0900 Subject: [PATCH 05/23] feat : Add SQLiteHelper.kt --- .../campus/tech/kakao/map/SQLiteHelper.kt | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt new file mode 100644 index 00000000..b9cdd32b --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt @@ -0,0 +1,41 @@ +package campus.tech.kakao.map + +import android.content.Context +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper + +class SQLiteHelper private constructor(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { + + companion object { + const val DATABASE_NAME = "SearchData.db" + const val DATABASE_VERSION = 1 + const val TABLE_NAME = "Data" + const val COL_NAME = "name" + const val COL_ADDRESS = "address" + const val COL_CATEGORY = "category" + + private var instance: SQLiteHelper? = null + + @Synchronized + fun getInstance(context: Context): SQLiteHelper { + if (instance == null) { + instance = SQLiteHelper(context.applicationContext) + } + return instance!! + } + } + + override fun onCreate(db: SQLiteDatabase) { + val createTable = "CREATE TABLE $TABLE_NAME (" + + "$COL_NAME TEXT PRIMARY KEY, " + + "$COL_ADDRESS TEXT, " + + "$COL_CATEGORY TEXT)" + db.execSQL(createTable) + } + + override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + db.execSQL("DROP TABLE IF EXISTS $TABLE_NAME") + onCreate(db) + } + +} From 60382ebff1d23ca0b0b09d39c6db6e00ff9a8dff Mon Sep 17 00:00:00 2001 From: SHEEN Date: Mon, 1 Jul 2024 16:13:09 +0900 Subject: [PATCH 06/23] feat : Add SQLiteDb.kt --- .../java/campus/tech/kakao/map/SQLiteDb.kt | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt index bb59bce4..6f519364 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt @@ -1,4 +1,42 @@ package campus.tech.kakao.map -class SQLiteDb { -} \ No newline at end of file +import android.content.ContentValues +import android.content.Context +import android.database.Cursor +import android.database.sqlite.SQLiteDatabase +import android.util.Log + +class SQLiteDb(context: Context) { + private val dbHelper: SQLiteHelper = SQLiteHelper.getInstance(context) + private val database: SQLiteDatabase = dbHelper.writableDatabase + + fun insertData(name: String, address: String, category: String): Any { + if (!isDataExists(name)) { + val values = ContentValues().apply { + put(SQLiteHelper.COL_NAME, name) + put(SQLiteHelper.COL_ADDRESS, address) + put(SQLiteHelper.COL_CATEGORY, category) + } + return database.insert(SQLiteHelper.TABLE_NAME, null, values) + } + return false + } + + fun isDataExists(name: String): Boolean { + val cursor = database.query( + SQLiteHelper.TABLE_NAME, + arrayOf(SQLiteHelper.COL_NAME), + "${SQLiteHelper.COL_NAME} = ?", + arrayOf(name), + null, + null, + null + ) + val exists = cursor.count > 0 + cursor.close() + return exists + } + + + +} From 210a7419e9015be5ce50eec185973fedf4bb2daf Mon Sep 17 00:00:00 2001 From: SHEEN Date: Mon, 1 Jul 2024 16:41:38 +0900 Subject: [PATCH 07/23] feat : Add logAllData for checking on log --- .../main/java/campus/tech/kakao/map/SQLiteDb.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt index 6f519364..893bda8e 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt @@ -36,6 +36,22 @@ class SQLiteDb(context: Context) { cursor.close() return exists } + fun getAllData(): Cursor { + return database.query(SQLiteHelper.TABLE_NAME, null, null, null, null, null, null) + } + + fun logAllData() { + val cursor = getAllData() + if (cursor.moveToFirst()) { + do { + val name = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_NAME)) + val address = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_ADDRESS)) + val category = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_CATEGORY)) + Log.d("DatabaseHelper", "Name: $name, Address: $address, Category : $category") + } while (cursor.moveToNext()) + } + cursor.close() + } From 4d764caab4b3c4b1900f53f82e2bef075934f960 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Mon, 1 Jul 2024 17:18:12 +0900 Subject: [PATCH 08/23] feat : Add checkRun fun for Add Data for only one --- .../campus/tech/kakao/map/MainActivity.kt | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) 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..572d867b 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,47 @@ package campus.tech.kakao.map +import android.content.Context 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) + checkRun() + + } + fun checkRun(){ + val checkRun = getSharedPreferences("", Context.MODE_PRIVATE) + .getBoolean("First",true) + if(checkRun){ + val dbHelper = SQLiteDb(this) + insertData(dbHelper) + getSharedPreferences("", Context.MODE_PRIVATE) + .edit() + .putBoolean("First", false) + .apply() + dbHelper.logAllData() + } + + } + } +fun insertData(dbHelper: SQLiteDb){ + for (i in 1..10) { + val name = "카페 $i" + val address = "서울 성동구 성수동 $i" + val category = "카페" + + dbHelper.insertData(name, address, category) + + + } + for (i in 1..10) { + val name = "약국 $i" + val address = "서울 강남구 대치동 $i" + val category = "약국" + + dbHelper.insertData(name, address, category) } } From 9ab3b8f1d7e4594f87a5679b8c879a75d44f9c42 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Tue, 2 Jul 2024 22:32:51 +0900 Subject: [PATCH 09/23] Refactor : function moved in the class --- .../campus/tech/kakao/map/MainActivity.kt | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) 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 572d867b..23569bd7 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -26,22 +26,23 @@ class MainActivity : AppCompatActivity() { } } - } -fun insertData(dbHelper: SQLiteDb){ - for (i in 1..10) { - val name = "카페 $i" - val address = "서울 성동구 성수동 $i" - val category = "카페" + fun insertData(dbHelper: SQLiteDb){ + for (i in 1..10) { + val name = "카페 $i" + val address = "서울 성동구 성수동 $i" + val category = "카페" - dbHelper.insertData(name, address, category) + dbHelper.insertData(name, address, category) - } - for (i in 1..10) { - val name = "약국 $i" - val address = "서울 강남구 대치동 $i" - val category = "약국" + } + for (i in 1..10) { + val name = "약국 $i" + val address = "서울 강남구 대치동 $i" + val category = "약국" - dbHelper.insertData(name, address, category) + dbHelper.insertData(name, address, category) + } + } } -} + From f29075bf868d28464d7b1356823ea2381a8ba7c5 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Wed, 3 Jul 2024 15:31:09 +0900 Subject: [PATCH 10/23] docs : Updated README.md --- README.md | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 482e7a6e..89f2b81c 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,25 @@ # android-map-keyword -# 1단계 - 로컬 데이터 +# 2단계 - 검색 ****** -## 카카오맵 클론 코딩을 위한 시작입니다. - -****** ## 기능 요구 사항 -### 검색어 입력 및 검색 결과를 표시할 기본 레이아웃을 구현 -### 검색에 사용될 데이터를 로컬 데이터베이스에 생성 +검색어를 입력하면 검색 결과 목록이 표시된다. + +검색 결과 목록은 세로 스크롤이 된다. + +입력한 검색어는 X를 눌러서 삭제할 수 있. + +검색 결과 목록에서 하나의 항목을 선택할 수 있다. + +선택된 항목은 검색어 저장 목록에 추가된다. + +저장된 검색어 목록은 가로 스크롤이 된다. + +저장된 검색어는 X를 눌러서 삭제할 수 있다. + +저장된 검색어는 앱을 재실행하여도 유지된다. From 74b8c69a4f22c1ef18b7895a1345751f9ccbe084 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Wed, 3 Jul 2024 16:49:39 +0900 Subject: [PATCH 11/23] feat : add recycleView searching --- .../campus/tech/kakao/map/MainActivity.kt | 104 +++++++++++++----- .../main/java/campus/tech/kakao/map/Place.kt | 7 ++ .../campus/tech/kakao/map/PlacesAdapter.kt | 37 +++++++ .../java/campus/tech/kakao/map/SQLiteDb.kt | 30 +++-- .../campus/tech/kakao/map/SQLiteHelper.kt | 1 - app/src/main/res/layout/activity_main.xml | 16 ++- app/src/main/res/layout/item_place.xml | 41 +++++++ 7 files changed, 198 insertions(+), 38 deletions(-) create mode 100644 app/src/main/java/campus/tech/kakao/map/Place.kt create mode 100644 app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt create mode 100644 app/src/main/res/layout/item_place.xml 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 23569bd7..0e086c72 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -2,47 +2,101 @@ package campus.tech.kakao.map import android.content.Context import android.os.Bundle -import android.util.Log +import android.view.View +import android.widget.TextView import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.widget.SearchView +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView class MainActivity : AppCompatActivity() { + private lateinit var searchView: SearchView + private lateinit var recyclerView: RecyclerView + private lateinit var noResultTextView: TextView + private lateinit var adapter: PlacesAdapter + private lateinit var databaseHelper: SQLiteDb + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - checkRun() + searchView = findViewById(R.id.searchView) + recyclerView = findViewById(R.id.recyclerView) + noResultTextView = findViewById(R.id.noResultTextView) + + databaseHelper = SQLiteDb(this) + recyclerView.layoutManager = LinearLayoutManager(this) + adapter = PlacesAdapter(listOf()) + recyclerView.adapter = adapter + + showNoResultMessage() + + searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String?): Boolean { + return false + } + + override fun onQueryTextChange(newText: String?): Boolean { + newText?.let { + if (it.isEmpty()) { + showNoResultMessage() + adapter.updateData(emptyList()) + } else { + searchPlaces(it) + } + } + return false + } + }) + + checkRun() } - fun checkRun(){ + + private fun checkRun() { val checkRun = getSharedPreferences("", Context.MODE_PRIVATE) - .getBoolean("First",true) - if(checkRun){ - val dbHelper = SQLiteDb(this) - insertData(dbHelper) - getSharedPreferences("", Context.MODE_PRIVATE) - .edit() - .putBoolean("First", false) - .apply() - dbHelper.logAllData() + .getBoolean("First", true) + if (checkRun) { + insertData() + getSharedPreferences("", Context.MODE_PRIVATE).edit().putBoolean("First", false).apply() + databaseHelper.logAllData() } - } - fun insertData(dbHelper: SQLiteDb){ - for (i in 1..10) { - val name = "카페 $i" - val address = "서울 성동구 성수동 $i" - val category = "카페" - - dbHelper.insertData(name, address, category) + private fun insertData() { + val dbHelper = SQLiteDb(this) + for (i in 1..10) { + dbHelper.insertData("카페 $i", "서울 성동구 성수동 $i", "카페") + dbHelper.insertData("약국 $i", "서울 강남구 대치동 $i", "약국") + } + } + private fun searchPlaces(query: String) { + val places = getPlacesFromDatabase(query) + if (places.isEmpty()) { + showNoResultMessage() + recyclerView.visibility = View.GONE + } else { + hideNoResultMessage() + recyclerView.visibility = View.VISIBLE + adapter.updateData(places) } - for (i in 1..10) { - val name = "약국 $i" - val address = "서울 강남구 대치동 $i" - val category = "약국" + } - dbHelper.insertData(name, address, category) + private fun getPlacesFromDatabase(query: String): List { + return databaseHelper.getAllData().filter { + it.name.contains(query, ignoreCase = true) || + it.address.contains(query, ignoreCase = true) || + it.category.contains(query, ignoreCase = true) } } + + private fun showNoResultMessage() { + noResultTextView.visibility = View.VISIBLE + recyclerView.visibility = View.GONE } + private fun hideNoResultMessage() { + noResultTextView.visibility = View.GONE + recyclerView.visibility = View.VISIBLE + } +} \ 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..df7fef87 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/Place.kt @@ -0,0 +1,7 @@ +package campus.tech.kakao.map; + +data class Place( + val name: String, + val address: String, + val category: String +) \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt b/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt new file mode 100644 index 00000000..bdba6239 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt @@ -0,0 +1,37 @@ +package campus.tech.kakao.map + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView + +class PlacesAdapter(private var places: List) : RecyclerView.Adapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlaceViewHolder { + val view = LayoutInflater.from(parent.context).inflate(R.layout.item_place, parent, false) + return PlaceViewHolder(view) + } + + override fun onBindViewHolder(holder: PlaceViewHolder, position: Int) { + val place = places[position] + holder.nameTextView.text = place.name + holder.addressTextView.text = place.address + holder.categoryTextView.text = place.category + } + + override fun getItemCount(): Int { + return places.size + } + + fun updateData(newPlaces: List) { + places = newPlaces + notifyDataSetChanged() + } + + class PlaceViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + val nameTextView: TextView = itemView.findViewById(R.id.nameTextView) + val addressTextView: TextView = itemView.findViewById(R.id.addressTextView) + val categoryTextView: TextView = itemView.findViewById(R.id.categoryTextView) + } +} diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt index 893bda8e..33db08c3 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt @@ -4,7 +4,6 @@ import android.content.ContentValues import android.content.Context import android.database.Cursor import android.database.sqlite.SQLiteDatabase -import android.util.Log class SQLiteDb(context: Context) { private val dbHelper: SQLiteHelper = SQLiteHelper.getInstance(context) @@ -36,23 +35,36 @@ class SQLiteDb(context: Context) { cursor.close() return exists } - fun getAllData(): Cursor { - return database.query(SQLiteHelper.TABLE_NAME, null, null, null, null, null, null) - } - fun logAllData() { - val cursor = getAllData() + fun getAllData(): List { + val places = mutableListOf() + val cursor = database.query( + SQLiteHelper.TABLE_NAME, + arrayOf(SQLiteHelper.COL_NAME, SQLiteHelper.COL_ADDRESS, SQLiteHelper.COL_CATEGORY), + null, + null, + null, + null, + null + ) + if (cursor.moveToFirst()) { do { val name = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_NAME)) val address = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_ADDRESS)) val category = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_CATEGORY)) - Log.d("DatabaseHelper", "Name: $name, Address: $address, Category : $category") + places.add(Place(name, address, category)) } while (cursor.moveToNext()) } cursor.close() - } - + return places + } + fun logAllData() { + val cursor = getAllData() + for (place in cursor) { + println("Name: ${place.name}, Address: ${place.address}, Category: ${place.category}") + } + } } diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt index b9cdd32b..c1a2e273 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt @@ -37,5 +37,4 @@ class SQLiteHelper private constructor(context: Context) : SQLiteOpenHelper(cont db.execSQL("DROP TABLE IF EXISTS $TABLE_NAME") onCreate(db) } - } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index cf998388..7ed8aa19 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -9,21 +9,31 @@ android:orientation="vertical"> + /> + + - + app:layout_constraintTop_toBottomOf="@id/searchView" /> diff --git a/app/src/main/res/layout/item_place.xml b/app/src/main/res/layout/item_place.xml new file mode 100644 index 00000000..294cc306 --- /dev/null +++ b/app/src/main/res/layout/item_place.xml @@ -0,0 +1,41 @@ + + + + + + + + + + From 5a2774538cf69ab44c6e752c581a0e3fef893c8b Mon Sep 17 00:00:00 2001 From: SHEEN Date: Wed, 3 Jul 2024 19:56:22 +0900 Subject: [PATCH 12/23] feat : Add class Item with changed file name --- .../java/campus/tech/kakao/map/{Place.kt => dataClass.kt} | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) rename app/src/main/java/campus/tech/kakao/map/{Place.kt => dataClass.kt} (65%) diff --git a/app/src/main/java/campus/tech/kakao/map/Place.kt b/app/src/main/java/campus/tech/kakao/map/dataClass.kt similarity index 65% rename from app/src/main/java/campus/tech/kakao/map/Place.kt rename to app/src/main/java/campus/tech/kakao/map/dataClass.kt index df7fef87..05097e29 100644 --- a/app/src/main/java/campus/tech/kakao/map/Place.kt +++ b/app/src/main/java/campus/tech/kakao/map/dataClass.kt @@ -4,4 +4,7 @@ data class Place( val name: String, val address: String, val category: String -) \ No newline at end of file +) +data class Item( + val id: Long, val searchingName: String +) From ce63ed19f01798667756aa519c1b0c7986f7a55c Mon Sep 17 00:00:00 2001 From: SHEEN Date: Thu, 4 Jul 2024 00:31:40 +0900 Subject: [PATCH 13/23] feat : Add DATA ID with AUTOINCREMENT --- .../main/java/campus/tech/kakao/map/SQLiteDb.kt | 16 +++++++++------- .../java/campus/tech/kakao/map/SQLiteHelper.kt | 4 +++- .../main/java/campus/tech/kakao/map/dataClass.kt | 3 ++- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt index 33db08c3..a15a2d6c 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt @@ -9,7 +9,7 @@ class SQLiteDb(context: Context) { private val dbHelper: SQLiteHelper = SQLiteHelper.getInstance(context) private val database: SQLiteDatabase = dbHelper.writableDatabase - fun insertData(name: String, address: String, category: String): Any { + fun insertData(name: String, address: String, category: String): Long { if (!isDataExists(name)) { val values = ContentValues().apply { put(SQLiteHelper.COL_NAME, name) @@ -18,7 +18,7 @@ class SQLiteDb(context: Context) { } return database.insert(SQLiteHelper.TABLE_NAME, null, values) } - return false + return -1 } fun isDataExists(name: String): Boolean { @@ -40,7 +40,7 @@ class SQLiteDb(context: Context) { val places = mutableListOf() val cursor = database.query( SQLiteHelper.TABLE_NAME, - arrayOf(SQLiteHelper.COL_NAME, SQLiteHelper.COL_ADDRESS, SQLiteHelper.COL_CATEGORY), + arrayOf(SQLiteHelper.COL_ID, SQLiteHelper.COL_NAME, SQLiteHelper.COL_ADDRESS, SQLiteHelper.COL_CATEGORY), null, null, null, @@ -50,10 +50,11 @@ class SQLiteDb(context: Context) { if (cursor.moveToFirst()) { do { + val id = cursor.getInt(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_ID)) val name = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_NAME)) val address = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_ADDRESS)) val category = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_CATEGORY)) - places.add(Place(name, address, category)) + places.add(Place(id, name, address, category)) } while (cursor.moveToNext()) } cursor.close() @@ -62,9 +63,10 @@ class SQLiteDb(context: Context) { } fun logAllData() { - val cursor = getAllData() - for (place in cursor) { - println("Name: ${place.name}, Address: ${place.address}, Category: ${place.category}") + val places = getAllData() + for (place in places) { + println("ID: ${place.id}, Name: ${place.name}, Address: ${place.address}, Category: ${place.category}") } } } + diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt index c1a2e273..d1706893 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt @@ -10,6 +10,7 @@ class SQLiteHelper private constructor(context: Context) : SQLiteOpenHelper(cont const val DATABASE_NAME = "SearchData.db" const val DATABASE_VERSION = 1 const val TABLE_NAME = "Data" + const val COL_ID = "id" const val COL_NAME = "name" const val COL_ADDRESS = "address" const val COL_CATEGORY = "category" @@ -27,7 +28,8 @@ class SQLiteHelper private constructor(context: Context) : SQLiteOpenHelper(cont override fun onCreate(db: SQLiteDatabase) { val createTable = "CREATE TABLE $TABLE_NAME (" + - "$COL_NAME TEXT PRIMARY KEY, " + + "$COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, " + + "$COL_NAME TEXT, " + "$COL_ADDRESS TEXT, " + "$COL_CATEGORY TEXT)" db.execSQL(createTable) diff --git a/app/src/main/java/campus/tech/kakao/map/dataClass.kt b/app/src/main/java/campus/tech/kakao/map/dataClass.kt index 05097e29..b53535ce 100644 --- a/app/src/main/java/campus/tech/kakao/map/dataClass.kt +++ b/app/src/main/java/campus/tech/kakao/map/dataClass.kt @@ -1,10 +1,11 @@ package campus.tech.kakao.map; data class Place( + val id: Int, val name: String, val address: String, val category: String ) data class Item( - val id: Long, val searchingName: String + val id: Int, val searchingName: String ) From bed845bbfcf85d92c4e6f56c274c9ffa8e9c31ee Mon Sep 17 00:00:00 2001 From: SHEEN Date: Thu, 4 Jul 2024 00:55:35 +0900 Subject: [PATCH 14/23] docs : created HistoryAdapter.kt --- app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt diff --git a/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt b/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt new file mode 100644 index 00000000..9b0acc06 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt @@ -0,0 +1,4 @@ +package campus.tech.kakao.map + +class HistoryAdapter { +} \ No newline at end of file From f6e49e6795db8b69f8ff0ed96556115e2f1b8659 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Thu, 4 Jul 2024 00:55:59 +0900 Subject: [PATCH 15/23] feat : RecyclerView for HistoryView --- app/src/main/res/layout/activity_main.xml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 7ed8aa19..f96a4dfe 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -16,6 +16,15 @@ app:iconifiedByDefault="false" app:queryHint="검색어를 입력해 주세요." /> + Date: Thu, 4 Jul 2024 00:56:18 +0900 Subject: [PATCH 16/23] docs : created item_history.xml --- app/src/main/res/layout/item_history.xml | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 app/src/main/res/layout/item_history.xml diff --git a/app/src/main/res/layout/item_history.xml b/app/src/main/res/layout/item_history.xml new file mode 100644 index 00000000..b5524c48 --- /dev/null +++ b/app/src/main/res/layout/item_history.xml @@ -0,0 +1,30 @@ + + + + + + + + From 63f53490cd7dcdd78eaccec578fa72505a8ec181 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Thu, 4 Jul 2024 01:20:35 +0900 Subject: [PATCH 17/23] feat : Send Data to SelectedData Table when clicked list --- .../campus/tech/kakao/map/MainActivity.kt | 6 +++-- .../campus/tech/kakao/map/PlacesAdapter.kt | 4 +++- .../java/campus/tech/kakao/map/SQLiteDb.kt | 13 +++++++---- .../campus/tech/kakao/map/SQLiteHelper.kt | 23 +++++++++++++++---- 4 files changed, 35 insertions(+), 11 deletions(-) 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 0e086c72..db5e7f45 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -26,7 +26,9 @@ class MainActivity : AppCompatActivity() { databaseHelper = SQLiteDb(this) recyclerView.layoutManager = LinearLayoutManager(this) - adapter = PlacesAdapter(listOf()) + adapter = PlacesAdapter(listOf()) { name -> + databaseHelper.insertIntoSelectedData(name) + } recyclerView.adapter = adapter showNoResultMessage() @@ -99,4 +101,4 @@ class MainActivity : AppCompatActivity() { noResultTextView.visibility = View.GONE recyclerView.visibility = View.VISIBLE } -} \ No newline at end of file +} diff --git a/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt b/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt index bdba6239..8a4445cc 100644 --- a/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt @@ -6,7 +6,7 @@ import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.RecyclerView -class PlacesAdapter(private var places: List) : RecyclerView.Adapter() { +class PlacesAdapter(private var places: List, private val onItemClick: (String) -> Unit) : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlaceViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_place, parent, false) @@ -18,6 +18,8 @@ class PlacesAdapter(private var places: List) : RecyclerView.Adapter Date: Thu, 4 Jul 2024 02:55:45 +0900 Subject: [PATCH 18/23] feat : get searchingHistory from SelectedData Table --- .../campus/tech/kakao/map/HistoryAdapter.kt | 48 ++++++++++++++++++- .../campus/tech/kakao/map/MainActivity.kt | 27 +++++++++-- .../java/campus/tech/kakao/map/SQLiteDb.kt | 32 +++++++++++++ app/src/main/res/layout/activity_main.xml | 7 +-- app/src/main/res/layout/item_history.xml | 6 +-- 5 files changed, 108 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt b/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt index 9b0acc06..18060ebd 100644 --- a/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt @@ -1,4 +1,48 @@ package campus.tech.kakao.map -class HistoryAdapter { -} \ No newline at end of file +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView + +class HistoryAdapter(private var historyList: List, private val itemClickListener: (String) -> Unit) : + RecyclerView.Adapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryViewHolder { + val view = LayoutInflater.from(parent.context).inflate(R.layout.item_history, parent, false) + return HistoryViewHolder(view) + } + + override fun onBindViewHolder(holder: HistoryViewHolder, position: Int) { + val item = historyList[position] + holder.bind(item) + } + + override fun getItemCount(): Int { + return historyList.size + } + + fun updateData(newHistoryList: List) { + historyList = newHistoryList + notifyDataSetChanged() + } + + inner class HistoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + private val historyTextView: TextView = itemView.findViewById(R.id.historyTextView) + + init { + itemView.setOnClickListener { + val position = adapterPosition + if (position != RecyclerView.NO_POSITION) { + val item = historyList[position] + itemClickListener(item) + } + } + } + + fun bind(item: String) { + historyTextView.text = item + } + } +} 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 db5e7f45..1a543ca7 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -15,6 +15,8 @@ class MainActivity : AppCompatActivity() { private lateinit var noResultTextView: TextView private lateinit var adapter: PlacesAdapter private lateinit var databaseHelper: SQLiteDb + private lateinit var historyRecyclerView: RecyclerView + private lateinit var historyAdapter: HistoryAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -23,16 +25,30 @@ class MainActivity : AppCompatActivity() { searchView = findViewById(R.id.searchView) recyclerView = findViewById(R.id.recyclerView) noResultTextView = findViewById(R.id.noResultTextView) + historyRecyclerView = findViewById(R.id.historyRecyclerView) databaseHelper = SQLiteDb(this) recyclerView.layoutManager = LinearLayoutManager(this) adapter = PlacesAdapter(listOf()) { name -> databaseHelper.insertIntoSelectedData(name) + updateHistory() } recyclerView.adapter = adapter + historyRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) + historyAdapter = HistoryAdapter(databaseHelper.getAllSelectedData()) { name -> + val deletedRows = databaseHelper.deleteFromSelectedData(name) + if (deletedRows > 0) { + updateHistory() + } else { + } + } + historyRecyclerView.adapter = historyAdapter - showNoResultMessage() + setupSearchView() + checkRun() + } + private fun setupSearchView() { searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { override fun onQueryTextSubmit(query: String?): Boolean { return false @@ -47,11 +63,9 @@ class MainActivity : AppCompatActivity() { searchPlaces(it) } } - return false + return true } }) - - checkRun() } private fun checkRun() { @@ -101,4 +115,9 @@ class MainActivity : AppCompatActivity() { noResultTextView.visibility = View.GONE recyclerView.visibility = View.VISIBLE } + + private fun updateHistory() { + val history = databaseHelper.getAllSelectedData() + historyAdapter.updateData(history) + } } diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt index 6710a67d..d36c8294 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt @@ -3,6 +3,7 @@ package campus.tech.kakao.map import android.content.ContentValues import android.content.Context import android.database.sqlite.SQLiteDatabase +import campus.tech.kakao.map.SQLiteHelper.Companion.COL_ID_2 class SQLiteDb(context: Context) { private val dbHelper: SQLiteHelper = SQLiteHelper.getInstance(context) @@ -74,4 +75,35 @@ class SQLiteDb(context: Context) { } return database.insert(SQLiteHelper.TABLE_NAME_2, null, values) } + + fun getAllSelectedData(): List { + val selectedData = mutableListOf() + val cursor = database.query( + SQLiteHelper.TABLE_NAME_2, + arrayOf(SQLiteHelper.COL_NAME_2), + null, + null, + null, + null, + null + ) + + if (cursor.moveToFirst()) { + do { + val name = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_NAME_2)) + selectedData.add(name) + } while (cursor.moveToNext()) + } + cursor.close() + + return selectedData.reversed() + } + + fun deleteFromSelectedData(id: String): Int { + return database.delete( + SQLiteHelper.TABLE_NAME_2, + "$COL_ID_2 = ?", + arrayOf(id.toString()) + ) + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f96a4dfe..ebad21ee 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -20,17 +20,18 @@ android:id="@+id/historyRecyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" - app:layout_constraintTop_toBottomOf="@+id/searchView" android:orientation="horizontal" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + app:layout_constraintTop_toBottomOf="@+id/searchView" tools:listitem="@layout/item_history" - android:visibility="gone" + android:visibility="visible" /> diff --git a/app/src/main/res/layout/item_history.xml b/app/src/main/res/layout/item_history.xml index b5524c48..40272069 100644 --- a/app/src/main/res/layout/item_history.xml +++ b/app/src/main/res/layout/item_history.xml @@ -1,7 +1,7 @@ @@ -11,12 +11,12 @@ android:layout_height="wrap_content" android:background="?attr/selectableItemBackgroundBorderless" android:src="@android:drawable/ic_menu_close_clear_cancel" - app:layout_constraintStart_toEndOf="@id/Name" + app:layout_constraintStart_toEndOf="@id/historyTextView" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> Date: Thu, 4 Jul 2024 03:19:29 +0900 Subject: [PATCH 19/23] feat : get searchingHistory from SelectedData Table --- .../campus/tech/kakao/map/MainActivity.kt | 2 +- .../tech/kakao/map/{dataClass.kt => Place.kt} | 5 +---- .../campus/tech/kakao/map/PlacesAdapter.kt | 1 - .../campus/tech/kakao/map/SQLiteHelper.kt | 19 ++++++++----------- 4 files changed, 10 insertions(+), 17 deletions(-) rename app/src/main/java/campus/tech/kakao/map/{dataClass.kt => Place.kt} (68%) 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 1a543ca7..8a132fc0 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -120,4 +120,4 @@ class MainActivity : AppCompatActivity() { val history = databaseHelper.getAllSelectedData() historyAdapter.updateData(history) } -} +} \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/dataClass.kt b/app/src/main/java/campus/tech/kakao/map/Place.kt similarity index 68% rename from app/src/main/java/campus/tech/kakao/map/dataClass.kt rename to app/src/main/java/campus/tech/kakao/map/Place.kt index b53535ce..90caa458 100644 --- a/app/src/main/java/campus/tech/kakao/map/dataClass.kt +++ b/app/src/main/java/campus/tech/kakao/map/Place.kt @@ -5,7 +5,4 @@ data class Place( val name: String, val address: String, val category: String -) -data class Item( - val id: Int, val searchingName: String -) +) \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt b/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt index 8a4445cc..f4b2eb54 100644 --- a/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/PlacesAdapter.kt @@ -18,7 +18,6 @@ class PlacesAdapter(private var places: List, private val onItemClick: (S holder.nameTextView.text = place.name holder.addressTextView.text = place.address holder.categoryTextView.text = place.category - holder.itemView.setOnClickListener {onItemClick(place.name)} } diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt index f3e2ac97..986c094b 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteHelper.kt @@ -15,7 +15,6 @@ class SQLiteHelper private constructor(context: Context) : SQLiteOpenHelper(cont const val COL_NAME = "name" const val COL_ADDRESS = "address" const val COL_CATEGORY = "category" - const val TABLE_NAME_2 = "SelectedData" const val COL_ID_2 = "id" const val COL_NAME_2 = "name" @@ -32,16 +31,6 @@ class SQLiteHelper private constructor(context: Context) : SQLiteOpenHelper(cont } override fun onCreate(db: SQLiteDatabase) { - createTables(db) - } - - override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { - db.execSQL("DROP TABLE IF EXISTS $TABLE_NAME") - db.execSQL("DROP TABLE IF EXISTS $TABLE_NAME_2") - createTables(db) - } - - private fun createTables(db: SQLiteDatabase) { val createTable = "CREATE TABLE $TABLE_NAME (" + "$COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, " + "$COL_NAME TEXT, " + @@ -54,4 +43,12 @@ class SQLiteHelper private constructor(context: Context) : SQLiteOpenHelper(cont "$COL_NAME_2 TEXT)" db.execSQL(createTable2) } + + override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + db.execSQL("DROP TABLE IF EXISTS $TABLE_NAME") + db.execSQL("DROP TABLE IF EXISTS $TABLE_NAME_2") + onCreate(db) + } + + } From 232c882ebf8cdc921adf62fd7439d35a9a0c1c47 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Thu, 4 Jul 2024 03:45:57 +0900 Subject: [PATCH 20/23] feat : delete each item_history when click image Button --- .../campus/tech/kakao/map/HistoryAdapter.kt | 27 +++++++++---------- .../campus/tech/kakao/map/MainActivity.kt | 12 ++++----- .../java/campus/tech/kakao/map/SQLiteDb.kt | 9 ++----- app/src/main/res/layout/item_history.xml | 6 ++--- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt b/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt index 18060ebd..f3c2db0d 100644 --- a/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt @@ -3,10 +3,11 @@ package campus.tech.kakao.map import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageButton import android.widget.TextView import androidx.recyclerview.widget.RecyclerView -class HistoryAdapter(private var historyList: List, private val itemClickListener: (String) -> Unit) : +class HistoryAdapter(private var historyList: MutableList, private val itemClickListener: (String) -> Unit) : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryViewHolder { @@ -15,8 +16,14 @@ class HistoryAdapter(private var historyList: List, private val itemClic } override fun onBindViewHolder(holder: HistoryViewHolder, position: Int) { - val item = historyList[position] - holder.bind(item) + val historyItem = historyList[position] + holder.bind(historyItem) + + holder.delButton.setOnClickListener { + itemClickListener(historyItem) + historyList.remove(historyItem) + notifyDataSetChanged() + } } override fun getItemCount(): Int { @@ -24,22 +31,14 @@ class HistoryAdapter(private var historyList: List, private val itemClic } fun updateData(newHistoryList: List) { - historyList = newHistoryList + historyList.clear() + historyList.addAll(newHistoryList) notifyDataSetChanged() } inner class HistoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { private val historyTextView: TextView = itemView.findViewById(R.id.historyTextView) - - init { - itemView.setOnClickListener { - val position = adapterPosition - if (position != RecyclerView.NO_POSITION) { - val item = historyList[position] - itemClickListener(item) - } - } - } + internal val delButton: ImageButton = itemView.findViewById(R.id.delButton) fun bind(item: String) { historyTextView.text = item 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 8a132fc0..19ec5aaf 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -26,7 +26,6 @@ class MainActivity : AppCompatActivity() { recyclerView = findViewById(R.id.recyclerView) noResultTextView = findViewById(R.id.noResultTextView) historyRecyclerView = findViewById(R.id.historyRecyclerView) - databaseHelper = SQLiteDb(this) recyclerView.layoutManager = LinearLayoutManager(this) adapter = PlacesAdapter(listOf()) { name -> @@ -34,17 +33,18 @@ class MainActivity : AppCompatActivity() { updateHistory() } recyclerView.adapter = adapter + historyRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) - historyAdapter = HistoryAdapter(databaseHelper.getAllSelectedData()) { name -> + val historyList = databaseHelper.getAllSelectedData() + historyAdapter = HistoryAdapter(historyList.toMutableList()) { name -> val deletedRows = databaseHelper.deleteFromSelectedData(name) if (deletedRows > 0) { - updateHistory() - } else { - } + updateHistory() } else { } } historyRecyclerView.adapter = historyAdapter setupSearchView() + checkRun() } @@ -120,4 +120,4 @@ class MainActivity : AppCompatActivity() { val history = databaseHelper.getAllSelectedData() historyAdapter.updateData(history) } -} \ No newline at end of file +} diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt index d36c8294..3fac0d4d 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt @@ -99,11 +99,6 @@ class SQLiteDb(context: Context) { return selectedData.reversed() } - fun deleteFromSelectedData(id: String): Int { - return database.delete( - SQLiteHelper.TABLE_NAME_2, - "$COL_ID_2 = ?", - arrayOf(id.toString()) - ) - } + + } diff --git a/app/src/main/res/layout/item_history.xml b/app/src/main/res/layout/item_history.xml index 40272069..c8255f8e 100644 --- a/app/src/main/res/layout/item_history.xml +++ b/app/src/main/res/layout/item_history.xml @@ -6,7 +6,7 @@ android:padding="8dp"> From 5df541b51404b0c212374852682f9d51aa55274f Mon Sep 17 00:00:00 2001 From: SHEEN Date: Thu, 4 Jul 2024 18:01:22 +0900 Subject: [PATCH 21/23] fix : did not delete specific DB COL, it fixed --- .../campus/tech/kakao/map/HistoryAdapter.kt | 19 +- .../campus/tech/kakao/map/MainActivity.kt | 206 +++++++++--------- .../java/campus/tech/kakao/map/SQLiteDb.kt | 17 +- 3 files changed, 128 insertions(+), 114 deletions(-) diff --git a/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt b/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt index f3c2db0d..17171a09 100644 --- a/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/HistoryAdapter.kt @@ -1,5 +1,6 @@ package campus.tech.kakao.map +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -7,7 +8,7 @@ import android.widget.ImageButton import android.widget.TextView import androidx.recyclerview.widget.RecyclerView -class HistoryAdapter(private var historyList: MutableList, private val itemClickListener: (String) -> Unit) : +class HistoryAdapter(private var historyList: MutableList>, private val itemClickListener: (Int) -> Unit) : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryViewHolder { @@ -16,13 +17,11 @@ class HistoryAdapter(private var historyList: MutableList, private val i } override fun onBindViewHolder(holder: HistoryViewHolder, position: Int) { - val historyItem = historyList[position] + val (id, historyItem) = historyList[position] holder.bind(historyItem) holder.delButton.setOnClickListener { - itemClickListener(historyItem) - historyList.remove(historyItem) - notifyDataSetChanged() + itemClickListener(id) } } @@ -30,12 +29,20 @@ class HistoryAdapter(private var historyList: MutableList, private val i return historyList.size } - fun updateData(newHistoryList: List) { + fun updateData(newHistoryList: List>) { historyList.clear() historyList.addAll(newHistoryList) notifyDataSetChanged() } + fun removeItemById(id: Int) { + val index = historyList.indexOfFirst { it.first == id } + if (index != -1) { + historyList.removeAt(index) + notifyItemRemoved(index) + } + } + inner class HistoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { private val historyTextView: TextView = itemView.findViewById(R.id.historyTextView) internal val delButton: ImageButton = itemView.findViewById(R.id.delButton) 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 19ec5aaf..a0a8f68f 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -1,123 +1,123 @@ -package campus.tech.kakao.map - -import android.content.Context -import android.os.Bundle -import android.view.View -import android.widget.TextView -import androidx.appcompat.app.AppCompatActivity -import androidx.appcompat.widget.SearchView -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView + package campus.tech.kakao.map + + import android.content.Context + import android.os.Bundle + import android.view.View + import android.widget.TextView + import androidx.appcompat.app.AppCompatActivity + import androidx.appcompat.widget.SearchView + import androidx.recyclerview.widget.LinearLayoutManager + import androidx.recyclerview.widget.RecyclerView + + class MainActivity : AppCompatActivity() { + private lateinit var searchView: SearchView + private lateinit var recyclerView: RecyclerView + private lateinit var noResultTextView: TextView + private lateinit var adapter: PlacesAdapter + private lateinit var databaseHelper: SQLiteDb + private lateinit var historyRecyclerView: RecyclerView + private lateinit var historyAdapter: HistoryAdapter + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + searchView = findViewById(R.id.searchView) + recyclerView = findViewById(R.id.recyclerView) + noResultTextView = findViewById(R.id.noResultTextView) + historyRecyclerView = findViewById(R.id.historyRecyclerView) + databaseHelper = SQLiteDb(this) + recyclerView.layoutManager = LinearLayoutManager(this) + adapter = PlacesAdapter(listOf()) { name -> + databaseHelper.insertIntoSelectedData(name) + updateHistory() + } + recyclerView.adapter = adapter + + historyRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) + val historyList = databaseHelper.getAllSelectedData() + historyAdapter = HistoryAdapter(historyList.toMutableList()) { id -> + val deletedRows = databaseHelper.deleteFromSelectedData(id) + if (deletedRows > 0) { + historyAdapter.removeItemById(id) + } else { } + } + historyRecyclerView.adapter = historyAdapter -class MainActivity : AppCompatActivity() { - private lateinit var searchView: SearchView - private lateinit var recyclerView: RecyclerView - private lateinit var noResultTextView: TextView - private lateinit var adapter: PlacesAdapter - private lateinit var databaseHelper: SQLiteDb - private lateinit var historyRecyclerView: RecyclerView - private lateinit var historyAdapter: HistoryAdapter + setupSearchView() + checkRun() + } - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) + private fun setupSearchView() { + searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String?): Boolean { + return false + } - searchView = findViewById(R.id.searchView) - recyclerView = findViewById(R.id.recyclerView) - noResultTextView = findViewById(R.id.noResultTextView) - historyRecyclerView = findViewById(R.id.historyRecyclerView) - databaseHelper = SQLiteDb(this) - recyclerView.layoutManager = LinearLayoutManager(this) - adapter = PlacesAdapter(listOf()) { name -> - databaseHelper.insertIntoSelectedData(name) - updateHistory() + override fun onQueryTextChange(newText: String?): Boolean { + newText?.let { + if (it.isEmpty()) { + showNoResultMessage() + adapter.updateData(emptyList()) + } else { + searchPlaces(it) + } + } + return true + } + }) } - recyclerView.adapter = adapter - historyRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) - val historyList = databaseHelper.getAllSelectedData() - historyAdapter = HistoryAdapter(historyList.toMutableList()) { name -> - val deletedRows = databaseHelper.deleteFromSelectedData(name) - if (deletedRows > 0) { - updateHistory() } else { } + private fun checkRun() { + val checkRun = getSharedPreferences("", Context.MODE_PRIVATE) + .getBoolean("First", true) + if (checkRun) { + insertData() + getSharedPreferences("", Context.MODE_PRIVATE).edit().putBoolean("First", false).apply() + databaseHelper.logAllData() + } } - historyRecyclerView.adapter = historyAdapter - - setupSearchView() - - checkRun() - } - private fun setupSearchView() { - searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { - override fun onQueryTextSubmit(query: String?): Boolean { - return false + private fun insertData() { + val dbHelper = SQLiteDb(this) + for (i in 1..10) { + dbHelper.insertData("카페 $i", "서울 성동구 성수동 $i", "카페") + dbHelper.insertData("약국 $i", "서울 강남구 대치동 $i", "약국") } + } - override fun onQueryTextChange(newText: String?): Boolean { - newText?.let { - if (it.isEmpty()) { - showNoResultMessage() - adapter.updateData(emptyList()) - } else { - searchPlaces(it) - } - } - return true + private fun searchPlaces(query: String) { + val places = getPlacesFromDatabase(query) + if (places.isEmpty()) { + showNoResultMessage() + recyclerView.visibility = View.GONE + } else { + hideNoResultMessage() + recyclerView.visibility = View.VISIBLE + adapter.updateData(places) } - }) - } - - private fun checkRun() { - val checkRun = getSharedPreferences("", Context.MODE_PRIVATE) - .getBoolean("First", true) - if (checkRun) { - insertData() - getSharedPreferences("", Context.MODE_PRIVATE).edit().putBoolean("First", false).apply() - databaseHelper.logAllData() } - } - private fun insertData() { - val dbHelper = SQLiteDb(this) - for (i in 1..10) { - dbHelper.insertData("카페 $i", "서울 성동구 성수동 $i", "카페") - dbHelper.insertData("약국 $i", "서울 강남구 대치동 $i", "약국") + private fun getPlacesFromDatabase(query: String): List { + return databaseHelper.getAllData().filter { + it.name.contains(query, ignoreCase = true) || + it.address.contains(query, ignoreCase = true) || + it.category.contains(query, ignoreCase = true) + } } - } - private fun searchPlaces(query: String) { - val places = getPlacesFromDatabase(query) - if (places.isEmpty()) { - showNoResultMessage() + private fun showNoResultMessage() { + noResultTextView.visibility = View.VISIBLE recyclerView.visibility = View.GONE - } else { - hideNoResultMessage() - recyclerView.visibility = View.VISIBLE - adapter.updateData(places) } - } - private fun getPlacesFromDatabase(query: String): List { - return databaseHelper.getAllData().filter { - it.name.contains(query, ignoreCase = true) || - it.address.contains(query, ignoreCase = true) || - it.category.contains(query, ignoreCase = true) + private fun hideNoResultMessage() { + noResultTextView.visibility = View.GONE + recyclerView.visibility = View.VISIBLE } - } - - private fun showNoResultMessage() { - noResultTextView.visibility = View.VISIBLE - recyclerView.visibility = View.GONE - } - private fun hideNoResultMessage() { - noResultTextView.visibility = View.GONE - recyclerView.visibility = View.VISIBLE - } - - private fun updateHistory() { - val history = databaseHelper.getAllSelectedData() - historyAdapter.updateData(history) + private fun updateHistory() { + val history = databaseHelper.getAllSelectedData() + historyAdapter.updateData(history) + } } -} diff --git a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt index 3fac0d4d..960fe9e2 100644 --- a/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt +++ b/app/src/main/java/campus/tech/kakao/map/SQLiteDb.kt @@ -76,11 +76,11 @@ class SQLiteDb(context: Context) { return database.insert(SQLiteHelper.TABLE_NAME_2, null, values) } - fun getAllSelectedData(): List { - val selectedData = mutableListOf() + fun getAllSelectedData(): List> { + val selectedData = mutableListOf>() val cursor = database.query( SQLiteHelper.TABLE_NAME_2, - arrayOf(SQLiteHelper.COL_NAME_2), + arrayOf(SQLiteHelper.COL_ID_2, SQLiteHelper.COL_NAME_2), null, null, null, @@ -90,8 +90,9 @@ class SQLiteDb(context: Context) { if (cursor.moveToFirst()) { do { + val id = cursor.getInt(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_ID_2)) val name = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteHelper.COL_NAME_2)) - selectedData.add(name) + selectedData.add(id to name) } while (cursor.moveToNext()) } cursor.close() @@ -99,6 +100,12 @@ class SQLiteDb(context: Context) { return selectedData.reversed() } - + fun deleteFromSelectedData(id: Int): Int { + return database.delete( + SQLiteHelper.TABLE_NAME_2, + "$COL_ID_2 = ?", + arrayOf(id.toString()) + ) + } } From b3a9562a0e52d5609038d4738bb7fc9f14ac4a38 Mon Sep 17 00:00:00 2001 From: SHEEN Date: Sun, 7 Jul 2024 20:51:49 +0900 Subject: [PATCH 22/23] fix : del app:layoutManager="..." --- app/src/main/res/layout/activity_main.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ebad21ee..1d79b2c4 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -21,7 +21,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" - app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:layout_constraintTop_toBottomOf="@+id/searchView" tools:listitem="@layout/item_history" android:visibility="visible" From 84a23bee9e12ec69f2846d42341573ccb3100c5e Mon Sep 17 00:00:00 2001 From: SHEEN Date: Sun, 7 Jul 2024 20:52:29 +0900 Subject: [PATCH 23/23] fix : using String.isNullOrEmpty() --- .../main/java/campus/tech/kakao/map/MainActivity.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) 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 a0a8f68f..bda2634b 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -55,13 +55,11 @@ } override fun onQueryTextChange(newText: String?): Boolean { - newText?.let { - if (it.isEmpty()) { - showNoResultMessage() - adapter.updateData(emptyList()) - } else { - searchPlaces(it) - } + if (newText.isNullOrEmpty()) { + showNoResultMessage() + adapter.updateData(emptyList()) + } else { + searchPlaces(newText) } return true }