Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何启用firebase脱机数据并将数据传递给recyclerview适配器?_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java 如何启用firebase脱机数据并将数据传递给recyclerview适配器?

Java 如何启用firebase脱机数据并将数据传递给recyclerview适配器?,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我在android聊天应用程序上工作,在我将这一行添加到myapplication classFirebaseDatabase.getInstance().setPersistenceEnabled(true)之前,它工作得很好若要启用获取脱机用户消息,将导致我的应用程序因此错误消息而崩溃 com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to lo

我在android聊天应用程序上工作,在我将这一行添加到myapplication class
FirebaseDatabase.getInstance().setPersistenceEnabled(true)之前,它工作得很好若要启用获取脱机用户消息,将导致我的应用程序因此错误消息而崩溃

com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long
如果我从myApplication类app work fine中删除了这一行,我知道这是因为我在pojo类中获取了数据,如何才能从firebase脱机功能中获益,并将这些数据传递给recyclerview适配器 我的获取消息方法

DatabasReference ref = FirebaseDatabase.getInstance().getreference().child("messages")
    private void fetchMessages() {
            if (mChildEventListener == null) {
                mChildEventListener = new ChildEventListener() {
                    @Override
                    public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                        if (snapshot.exists()) {
                            Messages messages = snapshot.getValue(Messages.class);
                            messagesList.add(messages);
                            adapter.notifyDataSetChanged();
                            mRecyclerView.smoothScrollToPosition(messagesList.size() - 1);
    
                        }
                    }
    
                    @Override
                    public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
    
    
                    }
ref.addChildEventLustener(mChildEventListener)
我的数据库结构

"Users": {
    "useId": {
      "name": m,
      "image": true
    }
"useId": {
      "name": n,
      "image": true
    }
}
"Messages" :{
   "mesageId" : {
      messsage:"hello"
      from: "MERjk5566699jjg"
      photoUrl : null
     date:145885665255
     seen :false
和消息类

public class Messages {
    private String message,from ,photoUrl;
    private long date;
    private boolean seen;

    public Messages() {
    }

    public Messages(String message, String from, String photoUrl, long date, boolean seen) {
        this.message = message;
        this.from = from;
        this.photoUrl = photoUrl;
        this.date = date;
        this.seen = seen;
    }

我回答我的问题是为了帮助任何人,如果他犯了这样的错误

com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long
使用
FirebaseDatabase.getInstance()时,setPersistenceEnabled(true)
发生错误的原因是我检索的数据如下

Messages messages = snapshot.getValue(Messages.class);
在脱机使用firebase时这样做是不对的,这是检索每个孩子的正确方法

String text = snapshot.child("message").getValue().toString;

然后创建类的实例并将其传递给适配器

请编辑您的问题并将您的数据库结构添加为JSON文件或至少是屏幕截图。除此之外,还请添加附加侦听器的引用。@Alex Mamo Edited请添加
Messages
类的内容。@Alex Mamo我已经添加了它。您确定数据库中没有名为“date”的属性,它保存的是字符串而不是长值吗?