简体   繁体   中英

Android Firebase, getKey of specific value in database

I have been searching for a few days, but couldn't find the solution for my problem.

I would like to get the key of the value that is selected in the spinner. The spinner contains an arraylist of the names in the database.

Button Testing = (Button)findViewById(R.id.Testing);
    Testing.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            p1ID = playerOneSpinner.getSelectedItem().toString();

            DatabaseReference db = (DatabaseReference) 
            FirebaseDatabase.getInstance().getReference().child("id");

            db.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot val : dataSnapshot.getChildren()){
                        data = val.toString();

                        Log.e("HALLO", data);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            Log.e("HALLO", p1ID + " <-- this is the spinner");

        }
    });

This is how my database lookslike

LadderCompetition
   - id
        -1
           Jeroen
        -2
           Franky
        -3
           Jesper
        -4
           Rob

输出 output of logcat

        DatabaseReference db = (DatabaseReference) 
        FirebaseDatabase.getInstance().getReference().child("id");

        db.orderByValue().equalTo(p1ID).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot val : dataSnapshot.getChildren()){
                    data = val.toString();

                    Log.e("HALLO", data);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM