Using Firebase Realtime Database:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map in Firebase Realtime database at line getUpdateData((Map)dataSnapshot.getValue());*
Firebase database structure is here:
This is what I did:
public ArrayList<NewsModel> alNotificationModel;
DatabaseReference ref;
ref = FirebaseDatabase.getInstance().getReference().child("notification");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
getUpdateData((Map<String, Object>) dataSnapshot.getValue());
pb1.setVisibility(View.GONE);
}
@Override
public void onCancelled(DatabaseError databaseError) {
//handle databaseError
}
});
private void getUpdateData(Map<String, Object> users) {
final ArrayList<NewsModel> alNotificationModel = new ArrayList<>();
for (Map.Entry<String, Object> entry : users.entrySet()) {
NewsModel notificationModel = new NewsModel();
Map singleUser = (Map) entry.getValue();
notificationModel.setNotificationId((Long) singleUser.get("notId"));
notificationModel.setNotificationTitle((String) singleUser.get("notTitle"));
notificationModel.setNotificationDescription((String) singleUser.get("notDescription"));
alNotificationModel.add(notificationModel);
}
}