Hello.
I want to be able to delete item on LongClick on View, but I need this item's key to do this. It there any way to get key for item in FirebaseRecyclerAdapter ?
I think what you're looking for is getRef(i).getKey() which is an instance method of FirebaseRecyclerAdapter. The i argument is the position, which you should have from populateViewHolder().
What is the alternative for FirestoreRecyclerAdapter ? There is no getRef() method available.
@binaryKarmic use getSnapshots().get(index) and then you can access the reference from there.
It will not help actually because it will give me the reference of my data model class.
onBindViewHolder(@NonNull final LiveChatViewHolder holder, int position, final @NonNull ModelClass object)
If i try to use getSnapshots().get(index) here then it will give me ModelClass whereas I need something like getRef(index) so that i can access the key of the node.
solved the issue by using the SnapshotParser
FirestoreRecyclerOptions< ModelClass > options = new FirestoreRecyclerOptions.Builder< ModelClass >().setQuery(query, new SnapshotParser< ModelClass >() {
@NonNull
@Override
public ModelClass parseSnapshot(@NonNull DocumentSnapshot snapshot) {
ModelClass modelClass = snapshot.toObject(ModelClass.class);
// so i wanted to add the key of the node as a field in the node object.
modelClass.setId(snapshot.getId());
return modelClass;
}
}).build();
@binaryKarmic that's a good solution!
Nice! FYI, you can also use this: getSnapshots().getSnapshot(index)
Oops yeah that's what I meant to type :sigh:
solved the issue by using the SnapshotParser
FirestoreRecyclerOptions< ModelClass > options = new FirestoreRecyclerOptions.Builder< ModelClass >().setQuery(query, new SnapshotParser< ModelClass >() { @NonNull @Override public ModelClass parseSnapshot(@NonNull DocumentSnapshot snapshot) { ModelClass modelClass = snapshot.toObject(ModelClass.class); // so i wanted to add the key of the node as a field in the node object. modelClass.setId(snapshot.getId()); return modelClass; } }).build();
the best solution
Most helpful comment
I think what you're looking for is
getRef(i).getKey()which is an instance method ofFirebaseRecyclerAdapter. Theiargument is the position, which you should have frompopulateViewHolder().