Search code examples
javafirebase-realtime-databasetextviewkeyspinner

How to Retrive Firebase key value based on spinner selection


I have a set of course details stored in the firebase Realtime Database. I have retrieved the course names into a spinner for user selection of a particular course. I wanted to get the key (course code) of that particular course to a TextView (in android java) when user select a course from the spinner from firebase database.

I have used the fetchDataForSpinner() method to retrieve the course name to the spinner. But I got confused on how to retrieve the course code to the TextView based on user selection.

I tried to get the key with get key but only the last course key was retrieved.

Firebase Data

enter image description here

  /*Firebase Data
   Courses
        Faculty of IT
          Year 01 Semester 01
            BMG11073:Entrepreneurship &Growing Business
            IT11013:"IT & Computing Fundamentals"
            IT11023:"Programming Fundamentals"
            IT11033:"Statistics"
            IT11041:"Psychology"
            IT11052:"Leadership and Communication Skills"*/


public void fetchDataForSpinner() {
        spinnerReference = FirebaseDatabase.getInstance().getReference("Courses");
        spinnerReference.child(faculty).child(semester).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(snapshot.exists()) {
                    for (DataSnapshot item : snapshot.getChildren()) {
                        spinnerCourseList.add(item.getValue().toString());
//                        String courseCodeFromKey = snapshot.getKey();
//                        editTextForCourseCode.setText(courseCodeFromKey);

                    }
                    adapterCourse.notifyDataSetChanged();
                }
                    else{
                        Toast.makeText(AddGradesMain.this, "Course Not Added!", Toast.LENGTH_SHORT).show();
                    }
                }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(AddGradesMain.this, "Course not added to the database!", Toast.LENGTH_SHORT).show();
            }
        });
    }

Solution

  • I don’t understand your question properly. If you provide the a json of your database that will be helpful for give the answer.

    But you can get key of a value like this:

    public void fetchDataForSpinner() {
            spinnerReference = FirebaseDatabase.getInstance().getReference("Courses");
            spinnerReference.child(faculty).child(semester).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if(snapshot.exists()) {
                        for (DataSnapshot item : snapshot.getChildren()) {
                            spinnerCourseList.add(item.getValue().toString());
                           String courseCodeFromKey = item.getKey();
    //                        editTextForCourseCode.setText(courseCodeFromKey);
    
                        }
                        adapterCourse.notifyDataSetChanged();
                    }
                        else{
                            Toast.makeText(AddGradesMain.this, "Course Not Added!", Toast.LENGTH_SHORT).show();
                        }
                    }
    
                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                    Toast.makeText(AddGradesMain.this, "Course not added to the database!", Toast.LENGTH_SHORT).show();
                }
            });
        }