question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It is unclear how to manage object keys in ListView / RecyclerView

See original GitHub issue

Possible doc enhancement?

Background

I came across this when trying to create a RecyclerView that launches a fragment with details about a list item when that list item is tapped. This would involve passing the key of the list item object to the details fragment so that details can be loaded. (Example: App shows a list of messages, user taps on a message and sees a new screen that shows details about the message)

Problem

There is no recommended pattern for tracking an object’s key in a FirebaseListAdapter/FirebaseRecyclerAdapter in the firebase-ui-database docs, which is the place I would go to learn how to use a RecyclerView with FirebaseUI.

This StackOverflow answer presents a way to retrieve the key of an object inside FirebaseListAdapter by adapter.getRef(position).getKey() but says that this method should be used as a last resort.

The docs for creating a model class show a field inside the model class, there called mUid, used to track a unique identifier. However, in this example the uid is different from the key of the object in the Firebase db. This means that querying is not a simple path like mDatabase.child("messages").child(key), but would instead have to be something like mDatabase.child("messages").orderByChild("uid").equalTo(mUid).

One solution might be to use mDatabase.child("posts").push().getKey() as the uid when the object is created, so that the uid matches the actual key in the db, and querying the object as a path like mDatabase.child("messages").child(key) is possible.

The other option I can see is to always use adapter.getRef(position).getKey(), to prevent duplicating the key inside the object.

Request

Can the docs supply a recommended approach to this, as it seems like a fairly common pattern?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pufcommented, Jan 27, 2017

In general I recommend hanging on to a DataSnapshot instead of a Map, since it’s a stronger (less generic) data type. The same applies for hanging on to a DatabaseReference over a key. It’s easy to mistake the meaning of a String, it’s almost impossible to do so for a DatabaseReference.

Not sure if that answers the question. Let me know if it doesn’t.

On Thu, Jan 26, 2017, 11:12 PM Sam Stern notifications@github.com wrote:

@puf https://github.com/puf can you elaborate on why adapter.getRef(position).getKey() is a “last resort” option?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/firebase/FirebaseUI-Android/issues/543#issuecomment-275530429, or mute the thread https://github.com/notifications/unsubscribe-auth/AA3w30EQOrrjgiyPawBgPE6OktaLmzFTks5rWRo-gaJpZM4LuIxd .

0reactions
cheskoscommented, Aug 8, 2017

@puf for President

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting object property in RecyclerView when item is clicked
With SetOnItemClickListener I'm able to intercept clicks on elements of the RecycleView, but I need to access to the properties of the binded ......
Read more >
RecyclerView - Android Developers
A Recycler is responsible for managing scrapped or detached item views for reuse. ... the new positions of views are unknown until the...
Read more >
Kivy Tutorial 4 : Kivy ListView & ListAdapter - YouTube
Code & Transcript : https://goo.gl/8hHa8MBest Kivy Book : http://amzn.to/2duPAFDSupport me on Patreon : https://www.patreon.com/derekbanasIn ...
Read more >
How to Access any Component Outside RecyclerView from ...
The title may be a little confusing but what we want to say is, suppose in an Android App, there's a RecyclerView, and...
Read more >
Android RecyclerView Tutorial with Kotlin - RayWenderlich.com
This object positions the RecyclerView's items and tells it when to recycle items that have transitioned off-screen. The ListView used to do ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found