Home > database >  Seting a Query in Firestore Cloud
Seting a Query in Firestore Cloud

Time:04-22

I have a question becouse i wrote this with a guide and it displays me all of the documents and i wanna that it displays only this with one i login in to my app and its will be nice when this wil be in 1 line and no i 2 seperates but it is not so necessary

   // Query the users collection
        val query = db.collection("users")
        val options = FirestoreRecyclerOptions.Builder<User>().setQuery(query, User::class.java)
            .setLifecycleOwner(this).build()
        val adapter = object: FirestoreRecyclerAdapter<User, UserViewHolder>(options) {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
             val view = LayoutInflater.from(this@MainActivity).inflate(android.R.layout.simple_list_item_2,parent,false)
                return UserViewHolder(view)
            }

            override fun onBindViewHolder(holder: UserViewHolder, position: Int, model: User) {
               val tvWelcome: TextView=holder.itemView.findViewById(android.R.id.text1)
                val tvName: TextView=holder.itemView.findViewById(android.R.id.text2)
                tvWelcome.text = model.welcome
                tvWelcome.text = model.displayName
                tvName.text = model.displayName
            }
        }

Firebase console

CodePudding user response:

val query = db.collection("users").whereEquelTo("firebasefieldName",youvalue)

Youvalue will write the data of the person you want to see. eg:"Sebastian"

firebasefieldName will be your domain name in firebase eg:displayName

Additional Information:

db.collection("users").whereEquelTo("fieldName1",youvalue).orderBy("fieldName2")

lists the listing in descending order of equal value but after doing that you will get an error message there will be a link to that error message go to it and confirm everything.

Please support if it worked.

CodePudding user response:

In this way, the data is updated instantly, for example, add a data to the table and it will appear in the list directly. You can adjust it yourself. Looks like I can't help any other way either. Will you still vote for me?

fun realtimeList(
            collectionPath: String,
            context: Context,
            cevapField: String,
            cevapdurum: Boolean,
            field: String,
            equel: String,
            query: Query.Direction
        ): LiveData<MutableList<Any>> {
            val mutableData = MutableLiveData<MutableList<Any>>()
            val docRef = Firebase.firestore.collection(collectionPath)
            docRef
                .whereEqualTo(field, equel)
                .whereEqualTo(cevapField, cevapdurum)
                .orderBy("date", query)
                .addSnapshotListener { querySnapshot, firebaseFirestoreException ->
                    firebaseFirestoreException?.let {
                        context.showShortToast(it.message.toString())
                        return@addSnapshotListener
                    }
                    val listData: MutableList<Any> = mutableListOf()
                    querySnapshot?.let {
                        for (document in it) {
                            val polyclinic = document.getString("policinic")
                            val title = document.getString("title")
                            val description = document.getString("descripiton")
                            val date: Long = document.get("date") as Long
                            val userid = document.getString("userid")
                            val quetinid = document.id
                            val cevapDurum = document.get("cevapdurum")
                            val questionio =
                                Questionio(
                                    polyclinic!!,
                                    title!!,
                                    description!!,
                                    SimpleDateFormat(date, "dd MMMM yyyy"),
                                    SimpleDateFormat(date, "HH:mm"),
                                    userid!!,
                                    quetinid,
                                    cevapDurum as Boolean
                                )
                            listData.add(questionio)
                        }
                        mutableData.value = listData
                    }
                }
            return mutableData
        }

Part of my list adapter class

inner class ListViewHolder(itemView: View) :
        RecyclerView.ViewHolder(itemView) {
        fun bindView(questionio: Questionio) {
            itemView.findViewById<TextView>(R.id.txt_policlinic).text = questionio.policlinic
            itemView.findViewById<TextView>(R.id.txt_title).text = questionio.title
            itemView.findViewById<TextView>(R.id.txt_description).text = questionio.description
            itemView.findViewById<TextView>(R.id.txt_date).text = questionio.date
            itemView.findViewById<TextView>(R.id.txt_time).text = questionio.time
            val txtCevapDurum = itemView.findViewById<TextView>(R.id.txt_CevapDurum)
            if (questionio.cevapdurum == true) {
                txtCevapDurum.setText(R.string.answered)
                txtCevapDurum.setTextColor(Color.GREEN)
            } else {
                txtCevapDurum.setText(R.string.not_answered)
                txtCevapDurum.setTextColor(Color.RED)
            }
        }

        init {
            itemView.setOnClickListener {
                listener.onItemClick(adapterPosition)
            }
        }
    }

videos https://www.youtube.com/watch?v=TIub9JMYhDs&t=226s&ab_channel=PhilippLackner

https://www.youtube.com/watch?v=Lfum5cO8LHA&ab_channel=SociedadAndroide

  • Related