0

I made a Firebase Realtime Database and integrated it with my Android app. However, I am not able to filter results based on a field.

This is my database:

{
    "news": {
        "15211": {
            "cat_id": 3,
            "cat_name": "ലോകം",
            "description": "ഒരുപക്ഷെ ജിഹാദികൾ പാകിസ്താന്റെ ഭരണം അട്ടിമറിച്ചു ...",
            "id": 15211,
            "image": "http://d3il9y9grvji6c.cloudfront.net/57ee50db0321d.jpg",
            "item_type": 1,
            "time": "1475236130",
            "title": " പാക്കിസ്ഥാന്റെ ന്യൂക്ലിയർ ചാവേറുകൾ ഇന്ത്യയെ ആക്രമിക്കുമോയെന്ന് ഹിലറിക്ക് പേടി",
            "url": "http://www.manoramaonline.com/news/just-in/hillary-clinton-fears-nuclear-suicide-bombers-from-pakistan.html",
            "url_text": "മനോരമ"
        },
        "15382": {
            "cat_id": 4,
            "cat_name": "കായികം",
            "description": "ന്യൂസിലന്‍ഡിനെതിരായ രണ്ടാം ക്രിക്കറ്റ് ടെസ്റ്റില്‍ ...",
            "id": 15382,
            "image": "http://d3il9y9grvji6c.cloudfront.net/57f0fda3abe93.jpg",
            "item_type": 1,
            "time": "1475411410",
            "title": "ഈഡനില്‍ ഇന്ത്യ വിജയം മണക്കുന്നു; ലീഡ് 339 ",
            "url": "http://www.reporterlive.com/2016/10/02/302271.html",
            "url_text": "റിപ്പോർട്ടർ"
        }
    }
}

The code I am using to get all items with cat_id = 3 is

DatabaseReference ref = database.getReference("news");
final Query queryRef = ref.orderByChild("cat_id").equalTo(3).limitToLast(10);
queryRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
  ...
  }
...
}

However, this always returns no result. What could be the issue?

AL.
  • 36,815
  • 10
  • 142
  • 281
Blaze Mathew
  • 185
  • 9
  • It's always best to post an exported json instead of a snapshot like that it will increase the chances of better and faster solutions. Please do consider this. How many children are there under news? – Nishant Dubey Oct 02 '16 at 12:22
  • 1
    do you get any result if you remove the `.limitToLast(10)` in the query? – Wilik Oct 02 '16 at 12:26
  • added exported json – Blaze Mathew Oct 02 '16 at 12:37
  • @Wilik yup its working thanks!! now how do i limit it to 10? – Blaze Mathew Oct 02 '16 at 12:43
  • you have to test this with more than 10 items. I haven't tried this so I can't confirm why your query didn't work. Looks like it's just not possible to use more than two `limitToFirst()`, `limitToLast()`, `startAt()`, `endAt()`, or `equalTo()`. More information about that in [this question](http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase) – Wilik Oct 02 '16 at 13:26
  • Please include the minimum code that reproduces the issue. Right now your `onDataChange` is empty, which means it won't do anything. Also make sure to [handle `onCancelled()`](http://stackoverflow.com/documentation/firebase/5548/how-do-i-listen-for-errors-when-accessing-the-database/22652/detect-errors-when-reading-data-on-android#t=201610021535197934615), since it may alert you to errors. – Frank van Puffelen Oct 02 '16 at 15:41

0 Answers0