2

I'm transitioning from SQLite to Firebase and in doing so, I have a lot of pre-existing data that I need to set into different views.

Initial view: An alphabetical list (no duplicates), showing letters for only those resources that exist below it (meaning if there is no resource that starts with X, X should not show up at all).

Secondary view: Once you tap on a letter, it expands accordion style to reveal resources (no duplicates) that start with that letter.

My Firebase query is right in that I'm getting the correct data, but how do I use it? Ideally what I'd like to do is get all the topics, drop them in a TreeSet to eliminate duplicates and sort them automatically, then do the same thing but just getting the first letter. I cant modify a set, or an array, or a list from an inner class, but I can't access the same if I instantiate from inside either (and it doesn't matter because it gets called fresh for every single object so it's always a new set).

Is there a best practice I'm missing?

mDatabase = FirebaseDatabase.getInstance();
mRootRef = mDatabase.getReference();
mQuoteRef = mRootRef.child("quotes");
Query topicQuery = mQuoteRef.orderByChild("Topic");

topicQuery.addChildEventListener(new ChildEventListener() {

      @Override
      public void onChildAdded(DataSnapshot dataSnapshot, String s) {
           TreeSet<String> topicList = new TreeSet<>(); <--obv not right
           TreeSet<String> firstLetterList = new TreeSet<>();
           GetQuoteInfo quote = dataSnapshot.getValue(GetQuoteInfo.class);
           if (quote.getTopic() != null) {
                topicList.add(quote.getTopic());
                firstLetterList.add(quote.getTopic().substring(0, 1));
           }
    }
}
magicalListThatContainsAllOfMyFirebaseDataThatICanNowManipulate;
KENdi
  • 7,576
  • 2
  • 16
  • 31
Jacob
  • 588
  • 1
  • 4
  • 14

1 Answers1

0

When you retrieve data from a Firebase database, you retrieve it as a Map, and not as a Set. So in order to solve your problem, change the way in which you retrieve the data. Because Firebase is a NoSQL database, better said a JSON database, everything is structured as key and value. So, change the Set with a Map and your problem will be solved.

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Well, I still have to define the `Map` outside of `onChildAdded` which prevents me from adding to it. – Jacob Jun 28 '17 at 17:21
  • You don't need to define outside the `onChildAdded()` method, you can define the `Map` inside, otherwise will be null, becasue Firebase loads and synchronizes data asynchronously. – Alex Mamo Jun 28 '17 at 17:26
  • But then, how do I access the `Map` outside of `onChildAdded` once all the data is added to the `Map`? This is assuming I can find a way to add to the `Map` without redefining it every single time. – Jacob Jun 28 '17 at 17:31
  • Simply, do what you need to do with the map inside that method. If you want to use that `Map` outside that method, pass the object of the map to another method and use it outside or see my answer from this [post](https://stackoverflow.com/questions/44726269/firebase-database-keys-to-arraylist). – Alex Mamo Jun 28 '17 at 17:35
  • Passing the data to another method makes sense. However, it's passing it for every single value, because `onChildAdded()` is called for every single object. What I need: A, B, C What I get: A, A, A, B, B, B, C, C, C – Jacob Jun 28 '17 at 17:59
  • 1
    Than i suggest you using `addListenerForSingleValueEvent()` and do the same thing in the `onDataChange()` method. – Alex Mamo Jun 28 '17 at 18:08