如何从 android 的 firebase 中检索 List 对象 [英] How to Retrieve a List object from the firebase in android
问题描述
我无法从 Firebase 检索列表.我存储它没有问题,但是一旦我尝试将 dataSnapshot.getValue() 转换为 ArrayList 我的应用程序就会崩溃,并给出一个异常:
I am having trouble retrieving a List from the Firebase. I have no trouble storing it, but as soon as I try to cast dataSnapshot.getValue() to ArrayList my app crashes, giving an exception:
HashMap 无法转换为 ArrayList
HashMap cannot be casted to ArrayList
但是当我尝试将其转换为 HashMap 时,它也会崩溃,并给出异常:
But when I tried to cast it to a HashMap, it also crashes, giving exception:
ArrayList 无法转换为 hashmap
ArrayList can't be casted to hashmap
需要帮助请!这是造成问题的代码:
Need help please! Here is the code that is creating the problem:
Fire.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<TaskDes> td = (ArrayList<TaskDes>) dataSnapshot.getValue()
notifyDataSetChanged();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
我想将 Firebase 中的所有数据作为一个列表进行检索.TaskDes 类包含三个字段:
I want to retrieve all the data in the Firebase as one List. The class TaskDes contains three fields:
class TaskDes { // definition
boolean done
String taskDescription
String taskTitle
}
推荐答案
您需要创建一个 GenericTypeIndicator 对象作为 DataSnapshot.getValue()
参数传递.
You need to create a GenericTypeIndicator object to pass as DataSnapshot.getValue()
parameter.
代码:
GenericTypeIndicator<List<String>> t = new GenericTypeIndicator<List<String>>() {};
List<String> yourStringArray = dataSnapshot.getValue(t);
这篇关于如何从 android 的 firebase 中检索 List 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!