Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Array List Object From Firebase

I added an ArrayList to the Firebase but I don't know how to get it back.

mDatabase.child("list").child("person").setValue(myValues);
//This is how I added it to my firebase

What I have tried is this. This is my error message

com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type Item

ValueEventListener postListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                for (DataSnapshot data : snapshot.getChildren()) {
                    GenericTypeIndicator<HashMap<String, Item>> t = new GenericTypeIndicator<HashMap<String, Item>>() {};

                    HashMap<String, Item> beans = data.getValue(t);

                    Set set = beans.entrySet();
                    Iterator i = set.iterator();

                    // Display elements
                    while (i.hasNext()) {
                        Map.Entry me = (Map.Entry) i.next();
                        Toast.makeText(getContext(), me.getKey() + ": ", Toast.LENGTH_LONG).show();
                        Toast.makeText(getContext(), me.getValue().toString(), Toast.LENGTH_LONG).show();
                        System.out.println(me.getValue());
                    }
                }

            }
            @Override
            public void onCancelled(DatabaseError firebaseError) {
                Log.e("The read failed: " ,firebaseError.getMessage());
            }
        };
        mDatabase.addValueEventListener(postListener);
like image 918
Jack Resone Avatar asked Oct 25 '16 22:10

Jack Resone


People also ask

What is getKey () in Firebase?

getKey() returns the key (last part of the path) of the location of the Snapshot. getReference() returns the Reference for the location that generated this Snapshot. getValue() returns the data contained in this Snapshot. hasChild() returns true if the specified child path has (non-null) data.

Can Firebase store array?

This is because Firebase does not support Arrays directly, but it creates a list of objects with integers as key names.

How do I get DataSnapshot data?

public DataSnapshot child (String path)Get a DataSnapshot for the location at the specified relative path. The relative path can either be a simple child key (e.g. 'fred') or a deeper slash-separated path (e.g. 'fred/name/first'). If the child location has no data, an empty DataSnapshot is returned.


1 Answers

Sorry if you were working on this all I needed to do was change the directory.

ValueEventListener postListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                GenericTypeIndicator<ArrayList<Item>> t = new GenericTypeIndicator<ArrayList<Item>>() {};
                ArrayList<Item> yourStringArray = snapshot.getValue(t);
                Toast.makeText(getContext(),yourStringArray.get(0).getName(),Toast.LENGTH_LONG).show();
            }
            @Override
            public void onCancelled(DatabaseError firebaseError) {
                Log.e("The read failed: " ,firebaseError.getMessage());
            }
        };
        mDatabase.child("list/user1").addValueEventListener(postListener);
like image 185
Jack Resone Avatar answered Oct 04 '22 01:10

Jack Resone