从 Firebase 拉取数据导致崩溃

我正在尝试从 Firebase 获取一些数据。我的 Firebase 结构如下:


"groups": {

   group-key (random key generated by firebase): {

      "creationDate": 

      "dropbox": 

      "endDate": 

      "evernote": 

      "googleDocs": 

      "members": {

               "0": user 1

               "1": user 2 

               "2": user 3 etc....

      }

     "projectName": 

     "subject": 

   }

}

我正在尝试groups使用以下代码从孩子那里拉下所有日期:


private List<DataService> grouplist;


mDatabase = FirebaseDatabase.getInstance();

mDBRef = mDatabase.getReference().child("groups");

grouplist = new ArrayList<>();


@Override

protected void onStart() {

    super.onStart();


grouplist.clear();


        mDBRef.addChildEventListener(new ChildEventListener() {

        @Override

        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {



            String creationDate = (String) dataSnapshot.child("creationDate").getValue();

            String dropbox = (String) dataSnapshot.child("dropbox").getValue();

            String endDate = (String) dataSnapshot.child("endDate").getValue();

            String evernote = (String) dataSnapshot.child("evernote").getValue();

            String googleDocs = (String) dataSnapshot.child("googleDocs").getValue();

            String members = (String) dataSnapshot.child("members").getValue();

            String projectName = (String) dataSnapshot.child("projectName").getValue();

            String subject = (String) dataSnapshot.child("subject").getValue();


            DataService dataService = new DataService();

            dataService.setCreationDate(creationDate);

            dataService.setDropbox(dropbox);

            dataService.setEndDate(endDate);

            dataService.setEvernote(evernote);

            dataService.setGoogleDocs(googleDocs);

            dataService.setmembers(members);

            dataService.setProjectName(projectName);

        }         

    });

}

但我收到此错误:

java.lang.ClassCastException: java.lang.Long 不能转换为 java.lang.String

我的猜测是问题出在“成员”子级并从那里获取用户。我是在正确的轨道上/出了什么问题还是有更好的方法来做到这一点?感谢所有帮助。

http://img.mukewang.com/61260cd50001f66209220744.jpg

慕标琳琳
浏览 184回答 3
3回答

ITMISS

问题是您在模型类creationDate中creationDate拥有String 类型的集合,而在数据库中则拥有long. 要解决此问题,请在您的模型类中将您的属性类型从 更改String为long:public long creationDate;并且请将相应的 setter 和 getter 更改为:public long getCreationDate() {&nbsp; &nbsp; return creationDate;}public void setCreationDate(long creationDate) {&nbsp; &nbsp; this.creationDate = creationDate;}构造函数也应该像这样改变:public DataService(String subject, String projectName, String endDate, String members, long creationDate, String dropbox, String evernote, String googleDocs) {&nbsp; &nbsp; this.subject = subject;&nbsp; &nbsp; this.projectName = projectName;&nbsp; &nbsp; this.endDate = endDate;&nbsp; &nbsp; this.members = members;&nbsp; &nbsp; this.creationDate = creationDate;&nbsp; &nbsp; this.dropbox = dropbox;&nbsp; &nbsp; this.evernote = evernote;&nbsp; &nbsp; this.googleDocs = googleDocs;}最后,只需更改以下代码行:String creationDate = (String) dataSnapshot.child("creationDate").getValue();到long creationDate = dataSnapshot.child("creationDate").getValue(Long.class);

收到一只叮咚

尝试使用这种方式public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataService creationDate = dataSnapshot.getValue(DataService.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grouplist.add(dataService);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, String.valueOf(grouplist));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;您正面临错误,因为您正试图将值对象强制转换为字符串类。所以你使用上面陈述的代码你只需你可以toString()用每个getValue()值方法调用该方法作为String creationDate = dataSnapshot.child("creationDate").getValue().toString();//convert land value to String value not into the String class object您还需要更改String member为List<User> member并更改其数据类型 List 的 setter 和 getter 方法的另一件事。请记住:我假设 User 是一个包含用户数据的类。如果它是一个简单的 String 那么你可以使用 String 而不是 User

侃侃尔雅

而不是这个&nbsp; &nbsp; &nbsp; &nbsp; String creationDate = (String) dataSnapshot.child("creationDate").getValue();&nbsp; &nbsp; &nbsp; &nbsp; String dropbox = (String) dataSnapshot.child("dropbox").getValue();&nbsp; &nbsp; &nbsp; &nbsp; String endDate = (String) dataSnapshot.child("endDate").getValue();&nbsp; &nbsp; &nbsp; &nbsp; String evernote = (String) dataSnapshot.child("evernote").getValue();&nbsp; &nbsp; &nbsp; &nbsp; String googleDocs = (String) dataSnapshot.child("googleDocs").getValue();&nbsp; &nbsp; &nbsp; &nbsp; String members = (String) dataSnapshot.child("members").getValue();&nbsp; &nbsp; &nbsp; &nbsp; String projectName = (String) dataSnapshot.child("projectName").getValue();&nbsp; &nbsp; &nbsp; &nbsp; String subject = (String) dataSnapshot.child("subject").getValue();像这样做&nbsp; &nbsp; &nbsp; &nbsp; String creationDate = dataSnapshot.child("creationDate").getValue().toString();&nbsp; &nbsp; &nbsp; &nbsp; String dropbox = dataSnapshot.child("dropbox").getValue().toString();&nbsp; &nbsp; &nbsp; &nbsp; String endDate = dataSnapshot.child("endDate").getValue().toString();&nbsp; &nbsp; &nbsp; &nbsp; String evernote = dataSnapshot.child("evernote").getValue().toString();&nbsp; &nbsp; &nbsp; &nbsp; String googleDocs = dataSnapshot.child("googleDocs").getValue().toString();&nbsp; &nbsp; &nbsp; &nbsp; String members = dataSnapshot.child("members").getValue().toString();&nbsp; &nbsp; &nbsp; &nbsp; String projectName = dataSnapshot.child("projectName").getValue().toString();&nbsp; &nbsp; &nbsp; &nbsp; String subject = dataSnapshot.child("subject").getValue().toString();toString() 用于将任何值转换为字符串。您不能像正在做的那样将任何内容转换为字符串。另外,这段代码&nbsp; &nbsp; &nbsp; &nbsp; DataService dataService = new DataService();&nbsp; &nbsp; &nbsp; &nbsp; dataService.setCreationDate(creationDate);&nbsp; &nbsp; &nbsp; &nbsp; dataService.setDropbox(dropbox);&nbsp; &nbsp; &nbsp; &nbsp; dataService.setEndDate(endDate);&nbsp; &nbsp; &nbsp; &nbsp; dataService.setEvernote(evernote);&nbsp; &nbsp; &nbsp; &nbsp; dataService.setGoogleDocs(googleDocs);&nbsp; &nbsp; &nbsp; &nbsp; dataService.setmembers(members);&nbsp; &nbsp; &nbsp; &nbsp; dataService.setProjectName(projectName);&nbsp; &nbsp; &nbsp; &nbsp; dataService.setSubject(subject);&nbsp; &nbsp; &nbsp; &nbsp; grouplist.add(dataService);可以用这个代替&nbsp; &nbsp; &nbsp; &nbsp; DataService dataService = new DataService(subject, projectName, endDate, members, creationDate, dropbox, evernote, googleDocs);&nbsp; &nbsp; &nbsp; &nbsp; grouplist.add(dataService);或这个,&nbsp; &nbsp; &nbsp; &nbsp; grouplist.add(new DataService(subject, projectName, endDate, members, creationDate, dropbox, evernote, googleDocs));因为您已经为此类创建了构造函数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java