10

When subclassing FirebaseListAdapter in FirebaseUI how can one get the obj key of the item clicked?

FirebaseListAdapter has the following method which gets itemId, but returns long. But I require the object key which is in the default string format.

public long getItemId(int i) {
    return (long)this.mSnapshots.getItem(i).getKey().hashCode();
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
srinivas
  • 4,778
  • 2
  • 32
  • 43

1 Answers1

34

The FirebaseListAdapter assumes that you always know the index/position of the item you are interacting with. Given the Android context this makes sense, since collection views are index based.

Once you know the position, you can call adapter.getRef(position) to get the Firebase reference to the object. On that reference, you can call getKey() to get the key. Although I recommend only doing that as a last resort.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Thanks! Yes that would work. But as I need it often to create referential paths all the time, I have actually just started storing keys and id's in the object itself, and find it very convenient pattern. – srinivas Oct 07 '15 at 19:38
  • 1
    We've been debating in the FirebaseUI team to store the **references** in the objects themselves, but so far have decided against it. The overhead of requiring a common base class or injector interface seemed to outweigh the advantages for the generic use-case. Of course, if you have a specific use-case the consideration can be different. I would still recommend keeping a `Firebase` ref instead of the key. Refs are lightweight and add a nice type-safe layer over just the key String. – Frank van Puffelen Oct 07 '15 at 20:02
  • Must be a good discussion. In my case it seems useful as I understand in many to many relational cases, you might need to create inverted indexes for each other in their own collections. so a firebase reference by default would be very helpful in this case, and probably is also frequently required in modelling many to many modelling requirements. – srinivas Oct 07 '15 at 20:46
  • Also storing Firebase refs is a great suggestions. Thanks! – srinivas Oct 07 '15 at 20:53
  • Given Firebase version 3... Which is now that mentioned `Firebase` object to keep reference of? – Joaquin Iurchuk Jun 04 '16 at 22:48