简体   繁体   中英

how to get string array from firebase realtime database

Firebase 实时数据库结构

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. im 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

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.

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM