Reputation: 221
I have app and I am using Firebase Realtime database. I need get all data from Search
and using this code:
myRef = FirebaseDatabase.getInstance().getReference();
Query query = myRef.orderByChild("Search");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
GenericTypeIndicator<ArrayList<String>> t = new GenericTypeIndicator<ArrayList<String>>(){};
test2 = dataSnapshot.getValue(t);
I need to get it all in ArrayList, but now i am getting below exception
com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap
Database structure:
How can I fix this? I need to get all data from Search
in ArrayList
.
If you need I can post all code from activity and logs.
Json:
{
"Search" : [ "Eggs", "Milk", "Apples", "Carrot", "Sugar", "Potato" ]
}
Upvotes: 2
Views: 904
Reputation: 138824
Assuming that the object
node is a direct child of your Firebase database root, to solve this, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference searchRef = rootRef.child("object").child("Search");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String value = ds.getValue(String.class);
list.add(value);
}
Log.d("TAG", list.toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
searchRef.addListenerForSingleValueEvent(valueEventListener);
The output will be:
Eggs
Milk
Apples
Carrot
Sugar
Potato
Upvotes: 3