Android Firebase,数据库中特定值的getKey

时间:2018-07-20 13:59:10

标签: java android firebase firebase-realtime-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");

        }
    });

这是我的数据库的样子

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

OutPut output of logcat

1 个答案:

答案 0 :(得分:0)

        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) {

            }
        });