Charlie
Charlie

Reputation: 359

does not recognize me getRef (position) is onBindViewHolder working with Firebase

I try to show a detail of the list but getref () does not recognize me,I want to get the key to go to another activity, help please, I'm new.

this is a class is not an activity

enter image description here

I use this code in an activity and it works very well

Upvotes: 1

Views: 1298

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138944

Inside onBindViewHolder method you can get the key using the following line of code:

String product_key = getItem(position);

getRef() is used to obtain a reference to the source location for the snapshot. So you can only use this method on a snapshot object. It returns a DatabaseReference and takes no argument. Below an example:

DatabaseReference ref = snapshot.child("users").getRef();

Upvotes: 1

Peter Haddad
Peter Haddad

Reputation: 80924

getRef() in firebase does not accept parameters, from the documentation:

public DatabaseReference getRef ()

Used to obtain a reference to the source location for this snapshot.

Returns:

A DatabaseReference corresponding to the location that this snapshot came from

You can do this:

Using an arrayList, called productList

final String product_key=productList.get(position).getKey();

more info here: https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot.html#getRef()

Upvotes: 1

Related Questions