【发布时间】:2020-04-24 18:21:09
【问题描述】:
我无法从 Firestore 文档字段中对 array 的值求和。
无论我做什么,我都会继续收到以下错误:
java.lang.ClassCastException: java.lang.Long 无法转换为 java.lang.Integer
我的要求如下:
我有一个 db Collection -> "ALLUSERS",每个文档 id 都是用户的电话号码。
在此文档下,我有集合“化学”和文档“摘要”。在此,我有一个带有点数的章节数组。
我可以通过 DocuementSnapshot.get(...the chapter...) 获取此详细信息。但是,一旦我尝试对数组中的值求和,就会出现上述错误。
下面是我尝试用流求和的代码。
for (DocumentSnapshot document : task.getResult()) {
String phonenumber = document.getString("phonenumb");
students.add(phonenumber);
chemistry.document(phonenumber)
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
Map<String, Object> map = task.getResult().getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().contains("chemchaps")) {
List<String> stringList = (List<String>) entry.getValue();
List<Integer> integerList = stringList.stream()
.map(Integer::valueOf).collect(Collectors.toList());
Integer maxValue = Collections.max(integerList);
Toast("Max Value " + integerList + " " + maxValue);
for(int i = 0; i < maxValue; i++){
String chapterNo = "Chapter"+String.valueOf(maxValue);
String chapternumb = "chapter"+String.valueOf(maxValue);
chemistry.document(phonenumber).collection("CHEMISTRY").document("Summary")
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
DocumentSnapshot documentSnapshot = task.getResult();
List<Integer> chaptersum = (List<Integer>) documentSnapshot.get(chapternumb);
long intsum = chaptersum.stream().mapToLong(Integer::longValue).sum();
//chaptersum.stream().reduce(0, (a, b)-> a+b);
//chaptersum.values().stream().mapToInt(Integer::intValue).sum();
Toast("Chapter Nos "+chapterNo + " "+ chaptersum+ " "+ intsum);
}
}
});
}
}
}
Toast("Map data " + map);
}
});
我在这一行得到错误:
long intsum = chaptersum.stream().mapToLong(Integer::longValue).sum();
感谢您的帮助
【问题讨论】:
标签: java android firebase arraylist google-cloud-firestore