6

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

3
  • How do you want to retrieve those values? Also which values do you want to get from the database? Commented Nov 3, 2018 at 13:27
  • i want to set each string array on different spinner in android. Commented Nov 3, 2018 at 13:28
  • Hey @ninjajack, if you found my answer useful, do mark it correct by clicking the V- type button next to it, this helps Stack Overflow future readers, and I'd appreciate that. Cheers! :) Commented Nov 9, 2018 at 3:20

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@ninjaJack, can you try the above code and tell what gets stored in category arraylist ?
its working i did bit change category.add(dataSnapshot.child(String.valueOf(i)).getValue(String.class));
Great, you can use similar code for other parts of your problem. Also, consider marking the answer as correct, I'd appreciate that. Cheers! :)
but what if i want to get all string arrays at a same time..and thanks a lot for help :D
@ninjajack ah, my bad for String.valueOf(i), great you spotted that :)
|
2

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));
        }

Comments

0

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.

Comments

0
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();
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.