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);
    }
}
5
  • users is defined where? The error is saying entry.getValue() is a list, not a map Commented Apr 10, 2018 at 6:37
  • @cricket_007 updated edit now pls check Commented Apr 10, 2018 at 6:42
  • check this link to get data stackoverflow.com/questions/46898/… Commented Apr 10, 2018 at 6:46
  • share your firebase database structure Commented Apr 10, 2018 at 6:54
  • @Lucky shared image Commented Apr 10, 2018 at 7:00

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

no you can use anything as key other than special characters
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.