I am trying to pass a String (userID
) from the onBindViewHolder
to my ViewHolder
class. The reason behind this is that I need the position to allocate the correct postID. This postID is used in the ViewHolder as a reference to set up a Firebase valueEventListener
. However, when running the app I get a NullPointerException
on the postID. Is this due to synchronization? Is the valueEventListener
called before postID is set?
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final PostViewHolder holder1 = (PostViewHolder) holder;
holder1.postID = getPostId(position)
//...
}
//...
public class PostViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private DatabaseReference mNoOfLikesRef;
public String postID;
//...
public PostViewHolder(View itemView, postClickListener listener){
super(itemView);
//This line causes the NullPointerException on postID
mNoOfLikesRef = FirebaseDatabase.getInstance().getReference().child("likes").child(postID);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//...
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
mNoOfLikesRef.addListenerForSingleValueEvent(valueEventListener);
}