Displaying all key when using getKey()

I want to get the key of what item I click but what happens in my code that when I click the one item it shows all the key instead of showing only the key on what I clicked

Here is my Firebase structure

Firebase Structure

and here is my code

public ViewHolderClass(@NonNull final View itemView) {
    super(itemView);
    subjectName=itemView.findViewById(R.id.subjectName);
    day=itemView.findViewById(R.id.day);
    time=itemView.findViewById(R.id.time);
    delete = itemView.findViewById(R.id.deleteIcon);
    subCounter = itemView.findViewById(R.id.subCounter);
    final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("rhein_subject");

    subjectName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final int pos = getAdapterPosition();
            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
                    Toast.makeText(itemView.getContext(), "Pos: " + dataSnapshot1.getKey(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    });

I don't know either where to put the pos I've been through the google for 3 days but still it hasn't answered my problem.



Read more here: https://stackoverflow.com/questions/64398152/displaying-all-key-when-using-getkey

Content Attribution

This content was originally published by Hermino Subrada at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: