how to get string array from firebase realtime database

大兔子大兔子 提交于 2020-01-24 22:42:20

问题


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


回答1:


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.




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



回答3:


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.



来源:https://stackoverflow.com/questions/53131751/how-to-get-string-array-from-firebase-realtime-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!