0

Firestore Database Accessing Code in Activitymain.xml

    db.collection("billNo") // I think want to add some code here to get the data!!//
            .get()
            .addOnSuccessListener {
                user.clear()
                for (document in it) {
                    user.add(User(
                            document.id,
                            document.data.get("billNo").toString().toInt(),
                            document.data.get("date").toString(),
                            document.data.get("name").toString(),
                            document.data.get("amount").toString().toInt(),
                            document.data.get("payment").toString()
                    )
                    )
                }

Database Structure

@Parcelize
    data class User(
    val id: String,
    val billNo: Int,
    val date: String,
    val name: String,
    val amount: Int,
    var payment:String
    ) : Parcelable

enter image description here

I Add billNo, date, name, amount, paid_or_unpaid data into the FIREBASE DATABASE. So when I click on searching I want to get details of the selected month in the RecyclerView. All RecyclerView code and other things working perfectly fine. Only I want to retrieve the data of the selected month.

When I click on the Respective Month & Year in the Following Activity (Screenshot is provided below) I want to get the details of bills in the respective month from the FIREBASE FIRE STORE DATA Base. I tried a lot. but I can't find a solution. I'm doing my project on Android Studio with KOTLIN programming Language.

enter image description here

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Abijith C
  • 13
  • 5
  • I'm not sure I understand. What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Jun 01 '21 at 08:04
  • When i add another month bill it will also show in the recyclerview. All datas entered to firebase is showing in the app. I want to restrict it to the selected month and year. So that i will get bill of the selected month. – Abijith C Jun 01 '21 at 09:10
  • Please edit your question and add your database structure as a screenshot. Please also indicate the exact data you want to get. – Alex Mamo Jun 01 '21 at 09:16
  • Yes. I corrected my Question. I need something like sql query for LIKE operator in FIREBASE using KOTLIN. so that i can search for the data and month. "SELECT * from tablename WHERE date LIKE %$month-$year%". I want a operation like this in FIREBASE using KOTLIN as language – Abijith C Jun 01 '21 at 10:32
  • I got it. I'll write you an answer right away. – Alex Mamo Jun 01 '21 at 11:09

1 Answers1

1

There is no LIKE operator in Firestore. So a query like this:

"SELECT * from tablename WHERE date LIKE %$month-$year%"

Is actually not possible. Because your "date" field is actually a String, when using small data sets you might consider using:

Or you can try using the newly Firebase Extension announced at Google I/O 2021 called:

Or directly in code:

However, there are two workarounds that can be used to achieve the same result. The first one would be to create another field in your User object called "monthYear". This property will hold a different value than the "date" property. For instance, if your "date" property holds the value of "03-May-2021", "monthYear" property will only hold the value of "May-2021". That been said, the Query will look like this in Kotlin:

val rootRef = FirebaseFirestore.getInstance()
val queryByMonthYear = rootRef.collection("billNo").whereEqualTo("monthYear", "May-2021")

The second solution would be to change the type of the "date" from String to Timestamp. Please see below how you can add a Timestamp to Firestore:

It's in Java but you can simply convert it in Kotlin. Then, you can use a Query with a call to startAt(startDate) and endAt(endDate).

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193