如何从android中的firebase中检索List对象 [英] How to Retrieve a List object from the firebase in android
问题描述
我无法从Firebase中检索列表。我没有麻烦存储它,但只要我试图将dataSnapshot.getValue()转换为ArrayList,我的应用程序崩溃,给出一个异常:
lockquote
<但是当我试图把它转换成一个HashMap的时候,它也崩溃,给出了异常:
HashMap不能被转换成ArrayList
/ b>
ArrayList不能被转换成hashmap
<请>请帮助!这里是创建问题的代码:
$ p $ Fire $ add
public void onDataChange(DataSnapshot dataSnapshot){
List< TaskDes> td =(ArrayList< TaskDes>)dataSnapshot.getValue()
notifyDataSetChanged();
}
@Override
public void onCancelled(FirebaseError firebaseError){
});
我想将Firebase中的所有数据都作为一个List来检索。类TaskDes包含三个字段:
pre $ class $ Task $ { b $ b字符串taskTitle
$ / code $
需要创建一个GenericTypeIndicator对象作为 DataSnapshot.getValue()
参数传递。
代码:
GenericTypeIndicator< List< String>> t = new GenericTypeIndicator< List< String>>(){};
列表< String> yourStringArray = dataSnapshot.getValue(t);
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 cannot be casted to ArrayList
But when I tried to cast it to a HashMap, it also crashes, giving exception:
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) {
}
});
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
}
You need to create a GenericTypeIndicator object to pass as DataSnapshot.getValue()
parameter.
Code:
GenericTypeIndicator<List<String>> t = new GenericTypeIndicator<List<String>>() {};
List<String> yourStringArray = dataSnapshot.getValue(t);
这篇关于如何从android中的firebase中检索List对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!