英文:
Retrieve an ArrayList from Firebase
问题
以下是翻译好的部分:
我在Firebase中有一个对象的ArrayList,我想要检索它。
我有两个ArrayList,一个是String,一个是Grade。当我尝试使用对象的数组时出现问题:
> java.lang.ClassCastException: java.util.HashMap 无法强制转换为 com.example.gradecalculator.Grade
mDocRef:
private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("myData/Arrays");
上传数组:
Map<String, Object> dataToSave = new HashMap<String, Object>();
dataToSave.put("StringsArray", stringGrades); // 保存String数组
dataToSave.put("ObjectsArray", grades); // 保存对象数组
mDocRef.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "文档已保存!");
Toast.makeText(getApplicationContext(), "列表已成功保存!", Toast.LENGTH_SHORT).show();
下载数组:
mDocRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
isListEmpty = false;
if (documentSnapshot.exists()) {
Map<String, Object> dataToLoad = (HashMap<String, Object>) documentSnapshot.getData();
stringGrades = (ArrayList<String>) dataToLoad.get("StringsArray");
grades = (ArrayList<Grade>) dataToLoad.get("ObjectsArray");
for (Grade grade : grades) { // <------------在这里失败
System.out.println(grade.toString());
}
第一次尝试使用Firebase。似乎它成功保存了数据到服务器。有什么建议吗?
英文:
I have an ArrayList of Objects in Firebase and I would like to retreive it.
I have 2 array lists. One String and one Grade. When I try to use the array of objects it has a problem:
> java.lang.ClassCastException: java.util.HashMap cannot be cast to com.example.gradecalculator.Grade
mDocRef:
private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("myData/Arrays");
Uploading the arrays:
Map<String, Object> dataToSave = new HashMap<String, Object>();
dataToSave.put("StringsArray", stringGrades); // Save Strings Array
dataToSave.put("ObjectsArray", grades); // Save Objects Array
mDocRef.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Document has been saved!");
Toast.makeText(getApplicationContext(), "List has been saved successfully!", Toast.LENGTH_SHORT).show();
Downloading the arrays:
mDocRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
isListEmpty = false;
if (documentSnapshot.exists()) {
Map<String, Object> dataToLoad = (HashMap<String, Object>) documentSnapshot.getData();
stringGrades = (ArrayList<String>) dataToLoad.get("StringsArray");
grades = (ArrayList<Grade>) dataToLoad.get("ObjectsArray");
for (Grade grade : grades) { // <------------Fails here
System.out.println(grade.toString());
}
First time I'm trying to use Firebase. It seems that it saves the data in the server successfully. Any advice?
答案1
得分: 2
DocumentSnapshot的get方法返回一个List,其中包含Map:
List<Map<String, Object>>
您可以创建另一个类,其中包含您的List<Grade>:
class GradeContainer{
private List<Grade> grades;
public GradeContainer(){}
public List<Grade> getGrades(){
return grades;
}
}
然后使用dataToLoad.toObject():
grades = dataToLoad.toObject(GradeContainer.class).getGrades();
您可以阅读以下文档获取更多信息:
以及以下教程:
英文:
The get method of DocumentSnapshot return a List of Map :
List<Map<String, Object>>
You can create another class with your List<Grade> :
class GradeContainer{
private List<Grade> grades;
public GradeContainer(){}
public List<Grade> getGrades(){
return grades;
}
}
And use dataToLoad.toObject() :
grades = List<Grade> dataToLoad.toObject(GradeContainer.class).getGrades();
You can read this documentation for more informations :
And this tutorial :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论