0

The case is like i want myShop cant view those product that already sold out. I have Quantity in my firebase.

private void setUpRecyclerView(){
        Query query = itemRef
                .orderBy("quantity", Query.Direction.DESCENDING)
                .whereEqualTo("sharerName", user)
                //.whereNotEqualTo("Quantity", 15)
                //.whereNotEqualTo("Quantity", "15")
                //.whereGreaterThan("Quantity", 0)
                //.whereGreaterThan("Quantity", "0")
                .whereEqualTo("sharerUID", uid);

        FirestoreRecyclerOptions<FoodModel> options = new FirestoreRecyclerOptions.Builder<FoodModel>()
                .setQuery(query, FoodModel.class)
                .build();

        iFoodSharedAdapter = new iFoodSharedAdapter(options);

        RecyclerView recyclerView = findViewById(R.id.isharedfood_recyclerview);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setAdapter(iFoodSharedAdapter);

    }

But it doesnt work.

There are 4 different ways to do it.

First and second both have the same error such as

.whereNotEqualTo("Quantity", 15)

It will show this error

error: cannot find symbol
                .whereNotEqualTo("Quantity", 15)
                ^
  symbol:   method whereNotEqualTo(String,int)
  location: class Query

Third and fourth ways

.whereGreaterThan("Quantity", 0)

Error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.fyp.fypfoodbank, PID: 20331
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp.fypfoodbank/com.fyp.fypfoodbank.Activities.iFoodSharedActivity}: java.lang.IllegalArgumentException: Invalid query. You have an inequality where filter (whereLessThan(), whereGreaterThan(), etc.) on field 'Quantity' and so you must also have 'Quantity' as your first orderBy() field, but your first orderBy() is currently on field 'quantity' instead.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2985)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3120)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1840)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:6878)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
     Caused by: java.lang.IllegalArgumentException: Invalid query. You have an inequality where filter (whereLessThan(), whereGreaterThan(), etc.) on field 'Quantity' and so you must also have 'Quantity' as your first orderBy() field, but your first orderBy() is currently on field 'quantity' instead.
        at com.google.firebase.firestore.Query.validateOrderByFieldMatchesInequality(Query.java:439)
        at com.google.firebase.firestore.Query.validateNewFilter(Query.java:471)
        at com.google.firebase.firestore.Query.whereHelper(Query.java:354)
        at com.google.firebase.firestore.Query.whereGreaterThan(Query.java:172)
        at com.fyp.fypfoodbank.Activities.iFoodSharedActivity.setUpRecyclerView(iFoodSharedActivity.java:118)
        at com.fyp.fypfoodbank.Activities.iFoodSharedActivity.onCreate(iFoodSharedActivity.java:69)
        at android.app.Activity.performCreate(Activity.java:7232)
        at android.app.Activity.performCreate(Activity.java:7221)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2965)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3120) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1840) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:207) 
        at android.app.ActivityThread.main(ActivityThread.java:6878) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876) 
I/Process: Sending signal. PID: 20331 SIG: 9
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
cococrunch
  • 15
  • 7
  • "*But it doesnt work*" isn't enough information. Please edit the question to explain what this code is supposed to do, and indicate what exactly isn't working the way you expect. If there is an error message, show it. I suggest reading: https://stackoverflow.com/help/how-to-ask – Doug Stevenson Oct 30 '20 at 04:17
  • Hi Doug Stevenson, I've updated more information above, Thank you for your reply. – cococrunch Oct 30 '20 at 05:40

1 Answers1

4

You are using an old version of the Firestore Android SDK. whereNotEqualTo was not added until version 21.7.0 of the firebase-firestore dependency. So, you will need to upgrade as described in the linked release notes. The latest version at the time of this answer is 22.0.0.

In your build.gradle, make sure you have at least:

implementation "com.google.firebase:firebase-firestore:21.7.0"
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441