1

I want to make it store private string keyy, but it does not save, what happens? I tried it with textview if it shows the data.

DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot nodo : dataSnapshot.getChildren()) {

            String key = nodo.getKey();
            Log.d("TAG", key);
            keyy = key;
            mSetting.setText(key); // yes, show data
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}

};
myRef.addListenerForSingleValueEvent(eventListener);
mSetting.setText(keyy); // No,show data
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • `keyy in mSetting.setText(keyy);` outside the `eventListener ` will be null until it gets value from `onDataChange()` that's why it shows data inside `onDataChange()` but not outside of it. You need to add all the keys into the list and show them in textview. – Mohammed Farhan Apr 07 '18 at 05:21
  • its not showing because its your keyy is not ready to store key and addListenerForSingleValueEvent run background while your next line of code get executed ! thats why it is not showing you need callback ! – iamkdblue Apr 07 '18 at 05:22
  • and then in what way can I solve it? and how do I apply the callback? Sorry, I'm a novice in this. – Anderson Jamanca Apr 07 '18 at 05:24
  • see the my answer i solved by without using callback! – iamkdblue Apr 07 '18 at 05:30
  • Possible duplicate of [How to return dataSnapshot value as a result of a method?](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method) – Alex Mamo Apr 07 '18 at 09:52
  • You cannot simply declare `keyy` variable as global and simply use it outside the `onDataChange()` method. Please see the duplicate, to see why and how can be solved this bahaviour. – Alex Mamo Apr 07 '18 at 09:53

2 Answers2

1

I hope this solve your problem

List<String> keyList = new ArrayList();    
    DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers");
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot nodo : dataSnapshot.getChildren()) {

                    String key = nodo.getKey();
                    keyList.add(key);

                }
               done(keyList);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}

        };
        myRef.addListenerForSingleValueEvent(eventListener);

        public void done(List k)
    {
      //show keys here
    }
iamkdblue
  • 3,448
  • 2
  • 25
  • 43
1

Do like this

    List<String> keyList=new ArrayList<>();
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot nodo : dataSnapshot.getChildren()) {

                    String key = nodo.getKey();
                    Log.d("TAG", key);

                /*Add all the keys to keyList here*/
                    keyList.add(key);

                }

        /*After the loop ends set the list of keys to textview like this*/
            if (keyList.size()>0)
            {
                for (String s : keyList)
                {
                mSetting.setText(mSetting.getText().toString()+"\n"+s);
                }
            }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}

};
myRef.addListenerForSingleValueEvent(eventListener);
Mohammed Farhan
  • 1,120
  • 8
  • 14