when data is changed in real time database(firebase) app crashes

Its data structue it don't just do with this data , error is same with this one too when i save data from other device into databse (firebase) app crashes where data is retrieved and set into recycle view in other device

onDataChange is realtime when data is changed it is automatically called
Notification Class



public class Notifications {
    private String Name;
    private String Email;
    private String Message;
    private String Time;
    private String Date;

    public Notifications() {

    }

    public Notifications(String Name, String Email, String Message, String Time, String Date) {
        this.Name = Name;
        this.Email = Email;
        this.Message = Message;
        this.Time = Time;
        this.Date = Date;
    }

    public String getName() {
        return Name;
    }

    public String getEmail() {
        return Email;
    }

    public String getMessage() {
        return Message;
    }

    public String getTime() {
        return Time;
    }

    public String getDate() {
        return Date;
    }
}
DatabaseReference userref = FirebaseDatabase.getInstance().getReference().child("Notification");
        userref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    notificationList.clear();
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {

                        Notifications notifications = userSnapshot.getValue(Notifications.class);

                        notificationList.add(notifications);

                    }
                    Collections.reverse(notificationList);
                    mAdapter = new Adapter_Notification(notificationList);
                    mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));

                    mRecyclerView.setAdapter(mAdapter);


                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

this error is shown

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Boolean to type com.example.simpleapplocker.Notifications

at the line

 notificationList.add(notifications);

after start is retrieves that data and shows in recycle view it mean data exists or maybe notification is created but other data is not uploaded and this function is called maybe??

Upvotes: 0

Views: 587

Answers (2)

Ashim Panda
Ashim Panda

Reputation: 11

Try this method, it worked for me

FirebaseDatabase database = FirebaseDatabase.getInstance();
                DatabaseReference myRef = database.getReference("Notification");
                myRef.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                        if(dataSnapshot.exist()) {
        Notifications notifications = userSnapshot.getValue(Notifications.class);
        
                                notificationList.add(notifications);
        
                            }
                            Collections.reverse(notificationList);
                            mAdapter = new 
      Adapter_Notification(notificationList);
                            mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
        
                            mRecyclerView.setAdapter(mAdapter);
     
                    }
        
                    @Override
                    public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        
                    }
        
                    @Override
                    public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
        
                    }
        
                    @Override
                    public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        
                    }
        
                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {
        
                    }
                });

Upvotes: 1

Muntasir Aonik
Muntasir Aonik

Reputation: 1957

Notifications notifications = userSnapshot.getValue(Notifications.class);

In this line, you are getting the error that says Boolean object can't be converted to Notifications. This may appear if you have set the value of that child to true or false (Without double quotes). See it again and try to add a double quote around the "true" or "false" that you want to set to the child value.

Upvotes: 0

Related Questions