Android/Firebase

[Firebase] CloudStore 복합쿼리

lipnus 2018. 8. 1. 23:01
반응형

원래 없었는데 CloudStore로 바뀌면서 Join같은 게 생겼다.

스피너로 도시와 나이를 선택하면 동시에 적용되서 검색되도록 하는 코드.


//도시선택 스피너
recyclerview_read_database_spinner_city.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(p0: AdapterView<*>?) {

}

override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
city = p0!!.getItemAtPosition(p2) as String
listBySpinner()
}
}

//나이선택 스피너
recyclerview_read_database_spinner_age.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(p0: AdapterView<*>?) {

}

override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
age = p0!!.getItemAtPosition(p2) as String
listBySpinner()
}

}
}


fun listBySpinner(){
if(city != null && age != null){
FirebaseFirestore.getInstance()
.collection("users")
.whereEqualTo("city", city)
.whereGreaterThanOrEqualTo("age", age!!.toInt())
.get().
addOnSuccessListener { querySnapshot ->
getArrayList.clear()
for(item in querySnapshot.documents){
var userDTO = item.toObject(UserDTO::class.java)
getArrayList.add(userDTO!!)
}
recyclerview_read_database.adapter.notifyDataSetChanged()
}.addOnFailureListener { exception ->
Log.e("HHTT", "Error: " + exception.toString());
}
}
}



요 부분이 복합쿼리다. 두개 동시에 검색

.whereEqualTo("city", city)
.whereGreaterThanOrEqualTo("age", age!!.toInt())


그냥 이렇게만 하면 에러나고, 파이어베이스 콘솔에서 설정을 해줘야 한다.

Log.e("HHTT", "Error: " + exception.toString());

이걸로 해놓으면 Logcat에 자동으로 링크가 뜬다.






전체코드

package com.example.lipnus.firebaseauth

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.TextView
import com.google.firebase.firestore.DocumentChange
import com.google.firebase.firestore.FirebaseFirestore
import kotlinx.android.synthetic.main.activity_read_database.*

class ReadDatabaseActivity : AppCompatActivity() {

var realTimeArrayList = arrayListOf<UserDTO>()
var realTimeKeyArrayList = arrayListOf<String>() //키값을 따로

var getArrayList = arrayListOf<UserDTO>()

var city : String? = null
var age: String? = null


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_read_database)

var userDTO = UserDTO("선필", 25, "서울")
// getArrayList.add(userDTO)
// getArrayList.add(userDTO)



recyclerview_read_database.adapter = ReadRecyclerViewAdapter(getArrayList)
recyclerview_read_database.layoutManager = LinearLayoutManager(this)
//Get
FirebaseFirestore.getInstance().collection("users").get().addOnSuccessListener { querySnapshot ->
for(item in querySnapshot.documents){
var userDTO = item.toObject(UserDTO::class.java)
getArrayList.add(userDTO!!)
}
recyclerview_read_database_realtime.adapter.notifyDataSetChanged()
}

//SnapShot
// recyclerview_read_database_realtime.adapter = ReadRecyclerViewAdapter(realTimeArrayList)
// recyclerview_read_database_realtime.layoutManager = LinearLayoutManager(this)
// FirebaseFirestore.getInstance().collection("users").addSnapshotListener { querySnapshot, firebaseFirestoreException ->
//
// realTimeArrayList.clear() //없에버리고
// for(item in querySnapshot!!.documents){
// var userDTO = item.toObject(UserDTO::class.java)
// realTimeArrayList.add(userDTO!!)
// }
// recyclerview_read_database.adapter.notifyDataSetChanged()
// }

recyclerview_read_database_realtime.adapter = ReadRecyclerViewAdapter(realTimeArrayList)
recyclerview_read_database_realtime.layoutManager = LinearLayoutManager(this)
FirebaseFirestore.getInstance().collection("users").addSnapshotListener { querySnapshot, firebaseFirestoreException ->

for(item in querySnapshot!!.documentChanges){
when(item.type){
DocumentChange.Type.ADDED -> {
realTimeArrayList.add(item.document.toObject(UserDTO::class.java))
realTimeKeyArrayList.add(item.document.id)
}
DocumentChange.Type.MODIFIED -> modifyItem(item.document.id, item.document.toObject(UserDTO::class.java))
DocumentChange.Type.REMOVED -> deleteItem(item.document.id)
}
}
recyclerview_read_database_realtime.adapter.notifyDataSetChanged()

}

//도시선택 스피너
recyclerview_read_database_spinner_city.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(p0: AdapterView<*>?) {

}

override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
city = p0!!.getItemAtPosition(p2) as String
listBySpinner()
}
}

//나이선택 스피너
recyclerview_read_database_spinner_age.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(p0: AdapterView<*>?) {

}

override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
age = p0!!.getItemAtPosition(p2) as String
listBySpinner()
}

}
}

fun listBySpinner(){
if(city != null && age != null){
FirebaseFirestore.getInstance()
.collection("users")
.whereEqualTo("city", city)
.whereGreaterThanOrEqualTo("age", age!!.toInt())
.get().
addOnSuccessListener { querySnapshot ->
getArrayList.clear()
for(item in querySnapshot.documents){
var userDTO = item.toObject(UserDTO::class.java)
getArrayList.add(userDTO!!)
}
recyclerview_read_database.adapter.notifyDataSetChanged()
}.addOnFailureListener { exception ->
Log.e("HHTT", "Error: " + exception.toString());
}
}
}

fun modifyItem(deleteKey: String, userDTO: UserDTO){
for( (position, item) in realTimeArrayList.withIndex() ){

}
}

fun deleteItem(deleteKey: String){
for( (position, item) in realTimeKeyArrayList.withIndex() ){
if(deleteKey == item){
realTimeArrayList.removeAt(position)
}
}
}



class ReadRecyclerViewAdapter(initList:ArrayList<UserDTO>) : RecyclerView.Adapter<RecyclerView.ViewHolder>(){

var list: ArrayList<UserDTO>? = initList

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
var view = LayoutInflater.from(parent!!.context).inflate(R.layout.item_recyclerview, parent, false)
return CustomViewHolder(view)
}

class CustomViewHolder(view: View?) : RecyclerView.ViewHolder(view) {
var textview_name = view!!.findViewById<TextView>(R.id.textView_name)
var textview_age = view!!.findViewById<TextView>(R.id.textView_age)
var textview_city = view!!.findViewById<TextView>(R.id.textView_city)
}

override fun getItemCount(): Int {
return list!!.size
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
var customViewHolder = holder as CustomViewHolder
customViewHolder.textview_name.text = list!!.get(position).name
customViewHolder.textview_age.text = list!!.get(position).age.toString()
customViewHolder.textview_city.text = list!!.get(position).city
}
}
}


반응형