SearchActivity.kt 2.73 KB
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
            }
        })
    }
}