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();
}

1 Answer 1

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.

5
  • 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 7, 2015 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. Oct 7, 2015 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 7, 2015 at 20:46
  • Also storing Firebase refs is a great suggestions. Thanks!
    – srinivas
    Oct 7, 2015 at 20:53
  • Given Firebase version 3... Which is now that mentioned Firebase object to keep reference of? Jun 4, 2016 at 22:48

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.