实时更改数据时,数据库(firebase)应用程序崩溃 [英] when data is changed in real time database(firebase) app crashes

查看:98
本文介绍了实时更改数据时,数据库(firebase)应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它不仅处理此数据,而且与 当我将其他设备中的数据保存到数据库(firebase)应用程序时崩溃,在该处检索数据并将其设置到其他设备中的回收视图中

it don't just do with this data , error is same with 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是实时的,在更改数据时会自动调用
通知类别

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) {

            }
        });

显示此错误

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

在线

 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??

推荐答案

尝试此方法,对我有用

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) {
        
                    }
                });

这篇关于实时更改数据时,数据库(firebase)应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆