我正在尝试从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);
dataService.setSubject(subject);
grouplist.add(dataService);
Log.d(TAG, String.valueOf(grouplist));
}
});
}
还有我的DataSerivice.java
public class DataService {
public String subject;
public String projectName;
public String endDate;
public String members;
public String creationDate;
public String dropbox;
public String evernote;
public String googleDocs;
public DataService() {
}
public DataService(String subject, String projectName, String endDate, String members, String creationDate, String dropbox, String evernote, String googleDocs) {
this.subject = subject;
this.projectName = projectName;
this.endDate = endDate;
this.members = members;
this.creationDate = creationDate;
this.dropbox = dropbox;
this.evernote = evernote;
this.googleDocs = googleDocs;
}
public String getCreationDate() {
return creationDate;
}
public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}
public String getDropbox() {
return dropbox;
}
public void setDropbox(String dropbox) {
this.dropbox = dropbox;
}
public String getEvernote() {
return evernote;
}
public void setEvernote(String evernote) {
this.evernote = evernote;
}
public String getGoogleDocs() {
return googleDocs;
}
public void setGoogleDocs(String googleDocs) {
this.googleDocs = googleDocs;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getmembers() {
return members;
}
public void setmembers(String members) {
this.members = members;
}
}
但是我遇到了这个错误:
java.lang.ClassCastException:无法将java.lang.Long强制转换为java.lang.String
我的猜测是问题出在“成员”子并从那里获取用户。我是在正确的轨道上吗/出什么问题了,还是有更好的方法来做到这一点?感谢所有帮助。
答案 0 :(得分:0)
尝试使用这种方式
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
DataService creationDate = dataSnapshot.getValue(DataService.class);
grouplist.add(dataService);
Log.d(TAG, String.valueOf(grouplist));
}
您正面临错误,因为您试图将值对象转换为字符串类。因此,您只需使用上面规定的代码,就可以简单地使用每个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
答案 1 :(得分:0)
代替此
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();
这样做
String creationDate = dataSnapshot.child("creationDate").getValue().toString();
String dropbox = dataSnapshot.child("dropbox").getValue().toString();
String endDate = dataSnapshot.child("endDate").getValue().toString();
String evernote = dataSnapshot.child("evernote").getValue().toString();
String googleDocs = dataSnapshot.child("googleDocs").getValue().toString();
String members = dataSnapshot.child("members").getValue().toString();
String projectName = dataSnapshot.child("projectName").getValue().toString();
String subject = dataSnapshot.child("subject").getValue().toString();
toString()用于将任何值转换为字符串。您不能像执行操作一样将任何内容强制转换为字符串。
此代码
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);
dataService.setSubject(subject);
grouplist.add(dataService);
可以替换为此
DataService dataService = new DataService(subject, projectName, endDate, members, creationDate, dropbox, evernote, googleDocs);
grouplist.add(dataService);
或者这个
grouplist.add(new DataService(subject, projectName, endDate, members, creationDate, dropbox, evernote, googleDocs));
因为您已经为此类创建了构造函数。
答案 2 :(得分:0)
问题是您的模型类中有creationDate
个类型为String的集合,而在数据库中,creationDate
是一个long
。要解决此问题,请在模型类中将属性的类型从String
更改为long
:
public long creationDate;
还请将相应的setter和getter更改为:
public long getCreationDate() {
return creationDate;
}
public void setCreationDate(long creationDate) {
this.creationDate = creationDate;
}
构造函数也应这样更改:
public DataService(String subject, String projectName, String endDate, String members, long creationDate, String dropbox, String evernote, String googleDocs) {
this.subject = subject;
this.projectName = projectName;
this.endDate = endDate;
this.members = members;
this.creationDate = creationDate;
this.dropbox = dropbox;
this.evernote = evernote;
this.googleDocs = googleDocs;
}
最后,只需更改以下代码行:
String creationDate = (String) dataSnapshot.child("creationDate").getValue();
到
long creationDate = dataSnapshot.child("creationDate").getValue(Long.class);