Reputation: 17
I've been stuck for two days on this previous segment (Java and Firebase).
I kept a class-type list in the database but when I try to get it to Firebase I get an error.
This is the code:
ArrayList<PostDetails_class> cPostList=new ArrayList<PostDetails_class>();
FirebaseDatabase.getInstance().getReference().addValueEventListener( new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
cPostList= (ArrayList<PostDetails_class>) snapshot.child( "application_details" ).child( "community_postsList" ).getValue();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
} );
This is an attempt to read data from the "cPost_List" list which is a list built from the "PostDetails_class" class. ("community_postsList" is a child that is a list of the "PostDetails_class" class):
PostDetails_class postDetails_class=cPostList.get( 0 ); //line 123 in main activity
the error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapp, PID: 1938
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.example.myapp.PostDetails_class
at com.example.myapp.MainActivity.makeList(MainActivity.java:123)
Upvotes: 0
Views: 1074
Reputation: 598976
When you call getValue()
without passing any argument, Firebase returns the native type for the data, which in this case is a Map<String, Object>
.
You need to tell Firebase what class to return by specifying it in the call to getValue
, for example with: getValue(PostDetails_class.class)
.
Firebase can't return a generic list in the way you're asking it to, because the type of the list is returned. I typically prefer looping of the child nodes, and extracting them one by one:
ArrayList<PostDetails_class> cPostList=new ArrayList<PostDetails_class>();
FirebaseDatabase.getInstance().getReference("application_details/community_postsList").addValueEventListener( new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot child: snapshot.getChildren()) {
PostDetails_class value = child.getValue(PostDetails_class.class);
cPostList.add(value);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException();
}
});
You'll note that I also changed the code to load data from the deeper node, instead of loading the entire database.
If you want to get the entire list in one go, that is possible but you will need a generic type indicator for the list. I recommend reading more on that in these questions about [firebase-realtime-database][android] generictypeindicator.
Upvotes: 2
Reputation: 40830
ArrayList<PostDetails_class> cPostList=new ArrayList<PostDetails_class>();
Firebase returns HashMap
, so you can't receive it into ArrayList
So you need to change the type of the cPostList
to HashMap<Integer, PostDetails_class>
HashMap<Integer, PostDetails_class> cPostList = new HashMap<>();
FirebaseDatabase.getInstance().getReference().addValueEventListener( new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
cPostList= (HashMap<Integer, PostDetails_class>) snapshot.child( "application_details" ).child( "community_postsList" ).getValue();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
} );
Upvotes: 0