0

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:

Click 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);
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Dhaval Jotaniya
  • 1,193
  • 2
  • 10
  • 19

2 Answers2

1

Here to resolve this kind of issue, the first thing you have to understand how firebase works. Like in current scenario firebase will give you List that's why you are getting ClassCastException because the data you have entered in database i.e in series 1,2... and so on here I am talking about keys in your data, if you change those keys with some prefix lets say not_1, not_2.... and so on it will definitely return you a hashmap of String and Object type. Please try that scenario and do let me know. Otherwise, if you don't want to change your database structure you can make a check whether the data snapshot is the instance of list or hashmap while extracting data. Here is the reference to the same problem you are facing hope this one helps you and parallelly I don't have any other reference to make you better understand, I will definitely add to this answer

This is how i had achieved

yashkal
  • 713
  • 5
  • 20
0

To solve this, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference notificationRef = rootRef.child("notification");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<NewsModel> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            NewsModel newsModel = ds.getValue(NewsModel.class);
            list.add(newsModel);
        }
        //Do what you need to do with this list
        Log.d("TAG", list.toString()); //To see is not emplty
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
notificationRef.addListenerForSingleValueEvent(valueEventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193