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);
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.
This is because Firebase does not support Arrays directly, but it creates a list of objects with integers as key names.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With