Search code examples
androidlistviewfirebaseandroid-recyclerviewgeofire

Geoquery to populate a ListView/RecyclerView


I'm developing an Android App with Firebase as a backend. So far it worked very smoothly and I loved it, but now I encountered a major roadblock. The app has to search for deals around a certain location and then populate a ListView (or a RecyclerView) accordingly. I've already included Geofire in my Firebase structure, having a main deals node and a geofire node with the same keys, and the Geoquery is returning them fine.

My problem is that the FirebaseListAdapter that I've grabbed from the Firebase examples doesn't have a single item add method, that it's what I'll need as the Geoquery will return only one key at time. After a long search, I've haven't been able to find an example of code that was dealing with the same issue, so I would like to ask for some advice on how, and which is the most efficient way to implement this.

Thanks

P.S.: my firebase structure:

{
  "deals" : {
    "-JikH6yjJ9TblE8_4fX4" : {
      "category" : "Food",
      "description" : "example description",
      "location" : "George St, Brisbane",
      "title" : "some title",
    }
  },
  "geofire" : {
    "-JikH6yjJ9TblE8_4fX4" : {
      ".priority" : "r7hgf2d639",
      "g" : "r7hgf2d639",
      "l" : [ -27.46268003, 153.03172851 ]
    }
  }
}

Solution

  • FirebaseListAdapter will only work for the simple case where you want to add all children from a Firebase location into a list. It is intended as a simple helper class. With more complicated examples like this, you will need to write your own ListAdapter.

    After receiving a key entered event for your GeoQuery you can add a value listener for the actual values in the deals node. You can then use those value events to populate and update your list view.

    Make sure to remove the value listener(s) on a key exit event or when you dispose of the list view.