ninja jack
ninja jack

Reputation: 115

how to get string array from firebase realtime database

Firebase Realtime database structure

databaseReference = FirebaseDatabase.getInstance().getReference("/sample");

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        Log.d(TAG, "onDataChange: dataSnapshot "+dataSnapshot.getValue());
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

I'm new to android app development and firebase as well. i m fetching data from sample node and getting DataSnapshot value like below.

{size=[Small, Large, Ex-Large], type=[Type 1, Type 2], color=[Red, Green, Blue], category=[T-Shirt, Jeans, Sweater]}

need some expect help, any suggestion will greatly appreciated.

Thanks

Upvotes: 6

Views: 11043

Answers (4)

Rebird
Rebird

Reputation: 1

FirebaseDatabase database = FirebaseDatabase.getInstance();     
    
    DatabaseReference refence = database.getReference();
    


    refence.addValueEventListener(new ValueEventListener()
            {

                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    // TODO Auto-generated method stub
                    
                    ArrayList array= new ArrayList<>();
                    
                    
                       for (DataSnapshot ds : snapshot.getChildren()){
                            
                           String data = ds.getValue().toString();
                           
                            array.add(data);
                                                        
                         
                            
                           

                            
                        }

                       System.out.println(array); 
                    
                }

                @Override
                public void onCancelled(DatabaseError error) {
                    // TODO Auto-generated method stub
                    
                }
        
            });
        

In my case String.class does not work instead .toString method works

String data = ds.getValue().toString();
    

Upvotes: 0

Sergio94
Sergio94

Reputation: 21

Firebase has no native support for arrays. If you store an array, it really gets stored as an "object" with integers as the key names.

// we send this

['hello', 'world']

// Firebase stores this

{0: 'hello', 1: 'world'}

Best Practices: Arrays in Firebase

// TRY THIS

@Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            youNameArray = new ArrayList<>();

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                String data = snapshot.getValue(String.class);
                youNameArray.add(data);
            }
            Log.v("asdf", "First data : " + youNameArray.get(0));
        }

Upvotes: 2

PradyumanDixit
PradyumanDixit

Reputation: 2375

To retrieve values separately, you can use a code like this:

databaseReference.child("category").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                   for (int i=0;i<3;i++) {
                     // category is an ArrayList you can declare above
                        category.add(dataSnapshot.child(String.valueOf(i)).getValue(String.class));

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) { // Do something for this

                }
            });

Similarly you can add values of other nodes in your other ArrayLists, just by changing the value of Childs in this code.

Upvotes: 3

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Something like this:

databaseReference = FirebaseDatabase.getInstance().getReference("/sample");

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot sampleSnapshot: dataSnapshot.getChildren()) {
            Log.d(TAG, "onDataChange: sampleSnapshot "+sampleSnapshot.getKey()+" = "+sampleSnapshot.getValue());
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

The difference is that in my answer I loop over dataSnapshot.getChildren() to get each individual sample snapshot. The sampleSnapshot.getValue() call should return a List.

Upvotes: 0

Related Questions