SearchActivity.kt
2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.example.vip
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
//import androidx.appcompat.widget.SearchView
import android.widget.SearchView
import android.widget.Toast
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.view.View
import android.widget.Button
import android.content.Intent
import android.util.Log
import androidx.core.content.ContextCompat
import com.google.firebase.database.*
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import kotlinx.android.synthetic.main.search.*
data class MemoItem(
val Target : String = "",
val Policy : String = "",
val Content : String = "",
val Link : String = ""
)
var database = FirebaseDatabase.getInstance().reference
class SearchActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.search)
val searchList = ArrayList<SearchItem>()
val searchView: SearchView
searchView = findViewById(R.id.searchForm)
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
// 검색 버튼이 눌러졌을 때 이벤트 처리
database.orderByChild("Policy").startAt("$query").endAt("$query"+"\uf8ff").addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
Toast.makeText(this@SearchActivity, "실패부분 : $query", Toast.LENGTH_SHORT).show()
}
override fun onDataChange(dataSnapshot: DataSnapshot) {
for(memoSnapshot in dataSnapshot.children){
val memo = memoSnapshot.getValue(MemoItem::class.java)
searchList.add(
SearchItem(
ContextCompat.getDrawable(this@SearchActivity, R.drawable.image01)!!, memo!!.Policy
)
)
}
val adapter=SearchAdapter(searchList)
searchRecyclerView.adapter=adapter
}
})
searchList.clear()
return true
}
override fun onQueryTextChange(newText: String): Boolean {
// 검색어가 변경되었을 때 이벤트 처리
return false
}
})
}
}