soonmyeong2

complete perpect search in NoSQL

1 package com.example.vip 1 package com.example.vip
2 2
3 +import android.content.Context
4 +import android.content.SharedPreferences
3 import android.os.Bundle 5 import android.os.Bundle
4 import androidx.appcompat.app.AppCompatActivity 6 import androidx.appcompat.app.AppCompatActivity
7 +import kotlinx.android.synthetic.main.activity_account_settings.*
5 8
6 class AccountSettingActivity : AppCompatActivity() { 9 class AccountSettingActivity : AppCompatActivity() {
7 override fun onCreate(savedInstanceState: Bundle?) { 10 override fun onCreate(savedInstanceState: Bundle?) {
8 super.onCreate(savedInstanceState) 11 super.onCreate(savedInstanceState)
9 setContentView(R.layout.activity_account_settings) 12 setContentView(R.layout.activity_account_settings)
13 +
14 + val prof: SharedPreferences = getSharedPreferences("policy_key", Context.MODE_PRIVATE)
15 + val editor: SharedPreferences.Editor=prof.edit()
16 +
17 + input_db.setOnClickListener{
18 + editor.putString("0", "[신한금융그룹] 신한 희망사회프로젝트 위기가정 재기지원사업 2년차 사업(2019년 5월~2020년 4월)")
19 + editor.putString("1", "2020년 동계 아름드리 대학생 해외봉사단원 모집")
20 + editor.putString("2", "초록우산 어린이재단 인재양성사업 - 2020 아이리더 모집공고")
21 + editor.putString("3", "(열매나눔복지재단, 처갓집양념치킨)2019 저소득가구 창업지원사업 처갓집양념치킨 창업자 모집공고(5.31-)")
22 + editor.putString("4", "[2017년도] 예비부부교실 / 신혼부부교실 : 연간일정")
23 + editor.putString("5", "신생아지원")
24 + editor.putString("6", "성폭력피해아동청소년 전용쉼터운영지원")
25 + editor.putString("7", "국가유공자등취업지원")
26 + editor.putString("8", "결혼이민자 통번역 서비스")
27 + editor.putString("9", "한부모가족자녀 교육비 지원")
28 + editor.commit()
29 + }
10 } 30 }
11 } 31 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -9,6 +9,7 @@ import androidx.core.content.ContextCompat.getSystemService ...@@ -9,6 +9,7 @@ import androidx.core.content.ContextCompat.getSystemService
9 import android.icu.lang.UCharacter.GraphemeClusterBreak.T 9 import android.icu.lang.UCharacter.GraphemeClusterBreak.T
10 import android.view.View 10 import android.view.View
11 import android.content.Intent 11 import android.content.Intent
12 +import android.content.SharedPreferences
12 import android.util.Log 13 import android.util.Log
13 import android.view.inputmethod.InputMethodManager 14 import android.view.inputmethod.InputMethodManager
14 import android.widget.* 15 import android.widget.*
...@@ -17,7 +18,10 @@ import com.google.firebase.database.* ...@@ -17,7 +18,10 @@ import com.google.firebase.database.*
17 import com.google.firebase.database.DatabaseReference 18 import com.google.firebase.database.DatabaseReference
18 import com.google.firebase.database.FirebaseDatabase 19 import com.google.firebase.database.FirebaseDatabase
19 import kotlinx.android.synthetic.main.search.* 20 import kotlinx.android.synthetic.main.search.*
20 - 21 +import com.google.firebase.database.DatabaseError
22 +import com.google.firebase.database.DataSnapshot
23 +import com.google.firebase.database.ValueEventListener
24 +import androidx.fragment.app.FragmentActivity
21 25
22 26
23 data class MemoItem( 27 data class MemoItem(
...@@ -34,6 +38,9 @@ class SearchActivity : AppCompatActivity() { ...@@ -34,6 +38,9 @@ class SearchActivity : AppCompatActivity() {
34 super.onCreate(savedInstanceState) 38 super.onCreate(savedInstanceState)
35 setContentView(R.layout.search) 39 setContentView(R.layout.search)
36 40
41 + val prof: SharedPreferences = getSharedPreferences("policy_key", Context.MODE_PRIVATE)
42 + val editor: SharedPreferences.Editor=prof.edit()
43 +
37 val searchList = ArrayList<SearchItem>() 44 val searchList = ArrayList<SearchItem>()
38 val no_search : ImageView = findViewById(R.id.no_matching) // 노서치이미지 45 val no_search : ImageView = findViewById(R.id.no_matching) // 노서치이미지
39 46
...@@ -55,14 +62,66 @@ class SearchActivity : AppCompatActivity() { ...@@ -55,14 +62,66 @@ class SearchActivity : AppCompatActivity() {
55 62
56 searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { 63 searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
57 override fun onQueryTextSubmit(query: String): Boolean { 64 override fun onQueryTextSubmit(query: String): Boolean {
65 +
66 + searchList.clear()
58 // 검색 버튼이 눌러졌을 때 이벤트 처리 67 // 검색 버튼이 눌러졌을 때 이벤트 처리
59 val layout : LinearLayout = findViewById(R.id.hashtag_linear) 68 val layout : LinearLayout = findViewById(R.id.hashtag_linear)
60 layout.setVisibility(View.GONE) 69 layout.setVisibility(View.GONE)
61 70
62 CloseKeyboard() 71 CloseKeyboard()
72 +
73 + // DB 내 목록에서 맞는거 찾는 부분
74 + val arr = arrayOf(0,1,2,3,4,5,6,7,8,9)
75 + val key = arrayListOf<String>()
76 + for (i in arr) {
77 + val temp = prof.getString("$i", "fail")
78 + val regex = "$query".toRegex()
79 + val matchResult : MatchResult? = regex.find(temp.toString())
80 + if (matchResult != null) {
81 + key.add(temp.toString())
82 + }
83 + }
84 + for (i in key) {
85 + val key_query = database.orderByChild("Policy").equalTo("$i")
86 + key_query.addListenerForSingleValueEvent(object : ValueEventListener {
87 + override fun onDataChange(dataSnapshot: DataSnapshot) {
88 + //Toast.makeText(getBaseContext(), dataSnapshot.getValue().toString(), Toast.LENGTH_SHORT).show()
89 +
90 +
91 + no_search.setVisibility(View.GONE)
92 + policy_scroll_view.setVisibility(View.VISIBLE)
93 +
94 +
95 + for (memoSnapshot in dataSnapshot.children){
96 + val memo = memoSnapshot.getValue(MemoItem::class.java)
97 +
98 + searchList.add(
99 + SearchItem(
100 + ContextCompat.getDrawable(this@SearchActivity, R.drawable.image01)!!, memo!!.Policy
101 + )
102 + )
103 + }
104 + val adapter=SearchAdapter(searchList)
105 + searchRecyclerView.adapter=adapter
106 +
107 + }
108 +
109 + override fun onCancelled(p0: DatabaseError) {
110 + }
111 + })
112 + }
113 + if (key.size == 0) {
114 + policy_scroll_view.setVisibility(View.GONE)
115 + no_search.setVisibility(View.VISIBLE)
116 +
117 + }
118 + //////////
119 +
120 +
121 +/*
63 database.orderByChild("Policy").startAt("$query").endAt("$query"+"\uf8ff").addListenerForSingleValueEvent(object : ValueEventListener { 122 database.orderByChild("Policy").startAt("$query").endAt("$query"+"\uf8ff").addListenerForSingleValueEvent(object : ValueEventListener {
64 override fun onCancelled(p0: DatabaseError) { 123 override fun onCancelled(p0: DatabaseError) {
65 - Toast.makeText(this@SearchActivity, "실패부분 : $query", Toast.LENGTH_SHORT).show() 124 + Toast.makeText(this@SearchActivity, "실패 : $query", Toast.LENGTH_SHORT).show()
66 } 125 }
67 override fun onDataChange(dataSnapshot: DataSnapshot) { 126 override fun onDataChange(dataSnapshot: DataSnapshot) {
68 if (dataSnapshot.exists() == false){ // 값 없을 때 127 if (dataSnapshot.exists() == false){ // 값 없을 때
...@@ -86,14 +145,15 @@ class SearchActivity : AppCompatActivity() { ...@@ -86,14 +145,15 @@ class SearchActivity : AppCompatActivity() {
86 val adapter=SearchAdapter(searchList) 145 val adapter=SearchAdapter(searchList)
87 searchRecyclerView.adapter=adapter 146 searchRecyclerView.adapter=adapter
88 } 147 }
89 - }) 148 + })*/
90 - searchList.clear()
91 149
92 return true 150 return true
151 +
93 } 152 }
94 153
95 154
96 155
156 +
97 override fun onQueryTextChange(newText: String): Boolean { 157 override fun onQueryTextChange(newText: String): Boolean {
98 // 검색어가 변경되었을 때 이벤트 처리 158 // 검색어가 변경되었을 때 이벤트 처리
99 return false 159 return false
......
...@@ -78,6 +78,12 @@ ...@@ -78,6 +78,12 @@
78 android:text="회원 탈퇴 (준비중)" 78 android:text="회원 탈퇴 (준비중)"
79 android:textSize="17dp" /> 79 android:textSize="17dp" />
80 80
81 + <Button
82 + android:id="@+id/input_db"
83 + android:layout_width="match_parent"
84 + android:layout_height="wrap_content"
85 + android:text="디비에 넣기" />
86 +
81 </LinearLayout> 87 </LinearLayout>
82 88
83 <com.google.android.material.bottomnavigation.BottomNavigationView 89 <com.google.android.material.bottomnavigation.BottomNavigationView
......