在数据更改之外使用Firebase数据库中的数据,Android Studio,Java

时间:2019-01-19 13:23:56

标签: java android firebase firebase-realtime-database

我正在尝试在onDataChange之外使用变量km1至km10。有什么办法吗?预先感谢您的宝贵时间!

这是我的代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);


        MatchBtn = (Button)findViewById(R.id.btnMatch);
        NoMatchBtn = (Button)findViewById(R.id.btnNoMatch);
        MatchBio = (TextView)findViewById(R.id.tvMatchBio);
        MatchAge = (TextView)findViewById(R.id.tvMatchAge);
        MatchName = (TextView)findViewById(R.id.tvMatchName);
        MatchPic = findViewById(R.id.ivMatch);

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();

        DatabaseReference dataREF = firebaseDatabase.getReference("Users").child(firebaseAuth.getUid());
         dataREF.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
        Profiel2 profiel2 = dataSnapshot.getValue(Profiel2.class);
        k1 = profiel2.getKnop1();
        k2 = profiel2.getKnop2();
        k3 = profiel2.getKnop3();
        k4 = profiel2.getKnop4();
        k5 = profiel2.getKnop5();
        k6 = profiel2.getKnop6();
        k7 = profiel2.getKnop7();
        k8 = profiel2.getKnop8();
        k9 = profiel2.getKnop9();
        k10 = profiel2.getKnop10();

        databasematchprocent = Integer.parseInt(profiel2.getDatabaseProcent());

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(SecondActivity.this, databaseError.getCode(), Toast.LENGTH_SHORT).show();
        }
        });




        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference usersRef = rootRef.child("Users");
        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    uid = ds.getKey();

                    if (uid.equals(firebaseAuth.getUid())){}
                    else{



                        DatabaseReference databaseReference = firebaseDatabase.getReference("Users").child(uid);
                         databaseReference.addValueEventListener(new ValueEventListener() {
                             @Override
                             public void onDataChange(DataSnapshot dataSnapshot) {
                                 Profiel2 profiel2 = dataSnapshot.getValue(Profiel2.class);
                                 km1 = profiel2.getKnop1();
                                 km2 = profiel2.getKnop2();
                                 km3 = profiel2.getKnop3();
                                 km4 = profiel2.getKnop4();
                                 km5 = profiel2.getKnop5();
                                 km6 = profiel2.getKnop6();
                                 km7 = profiel2.getKnop7();
                                 km8 = profiel2.getKnop8();
                                 km9 = profiel2.getKnop9();
                                 km10 = profiel2.getKnop10();


                             }

                             @Override
                             public void onCancelled(DatabaseError databaseError) {
                                 Toast.makeText(SecondActivity.this, databaseError.getCode(), Toast.LENGTH_SHORT).show();
                             }
                         });
                            //Where I want to use both km1 to km10 and k1 to k10
                        Log.e("KNOPPEN", "k1= "+ k1);
                        Log.e("KNOPPEN", "k2= "+ k2);
                        Log.e("KNOPPEN", "k3= "+ k3);
                        Log.e("KNOPPEN", "k4= "+ k4);
                        Log.e("KNOPPEN", "k5= "+ k5);
                        Log.e("KNOPPEN", "k6= "+ k6);
                        Log.e("KNOPPEN", "k7= "+ k7);
                        Log.e("KNOPPEN", "k8= "+ k8);
                        Log.e("KNOPPEN", "k9= "+ k9);
                        Log.e("KNOPPEN", "k20= "+ k10);
                            Log.e("KNOPPEN", "km1= "+ km1);
                            Log.e("KNOPPEN", "km2= "+ km2);
                            Log.e("KNOPPEN", "km3= "+ km3);
                            Log.e("KNOPPEN", "km4= "+ km4);
                            Log.e("KNOPPEN", "km5= "+ km5);
                            Log.e("KNOPPEN", "km6= "+ km6);
                            Log.e("KNOPPEN", "km7= "+ km7);
                            Log.e("KNOPPEN", "km8= "+ km8);
                            Log.e("KNOPPEN", "km9= "+ km9);
                            Log.e("KNOPPEN", "km20= "+ km10);
                            //All log.e request for km1 to km10 come back with 0
                            //While log.e request for k1 to k10 come back with the correct value
                                list.add(uid);

                            }

                    }}




                    ArrayCounter = (list.size());
                    int Matchpicker = (int) (Math.random() * ArrayCounter);
                    MatchUID = list.get(Matchpicker);



                DatabaseReference databaseReference2 = firebaseDatabase.getReference("Users").child(MatchUID);
                firebaseStorage = FirebaseStorage.getInstance();
                StorageReference storageReference = firebaseStorage.getReference();
                storageReference.child(MatchUID).child("Images/Profile Pic").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        Picasso.get().load(uri).fit().centerCrop().into(MatchPic);
                    }
                });
                databaseReference2.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
                        MatchName.setText("Name: " + userProfile.getUserName());
                        MatchAge.setText("Age: " + userProfile.getUserAge());
                        MatchBio.setText("Bio: " + userProfile.getUserBio());
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Toast.makeText(SecondActivity.this, databaseError.getCode(), Toast.LENGTH_SHORT).show();
                    }
                });

                MatchBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
                    DatabaseReference matchesREF = firebaseDatabase.getReference("Matches").child(firebaseAuth.getUid());
                    addMatch AddMatch = new addMatch(MatchUID);
                    matchesREF.setValue(AddMatch);
                    Toast.makeText(SecondActivity.this, "Match!", Toast.LENGTH_SHORT).show();
                }
                });

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        };
        usersRef.addListenerForSingleValueEvent(valueEventListener);
    }

我也不了解为什么我能够将变量k1到k10放在onDataChange之外,但是将变量km1到km10除外。我想在onCancelled之后使用变量k1到k10和km1到km10。该活动的目的是比较当前用户的k1到k10与数据库中随机用户的km1到km10。

0 个答案:

没有答案