如何将 Firebase 数组转换为 android 中的集合
How to convert Firebase array in to collection in android
firebase=new Firebase("https://flickering-inferno-4404.firebaseio.com/User/awosome");
firebase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String temp=dataSnapshot.getValue().toString();
GenericTypeIndicator<List<User>> t=new GenericTypeIndicator<List<User>>(){};
List<User> userList=dataSnapshot.getValue(t);
if (userList!=null){
userList.get(0).getBirthYear();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
并且用户模型是
public class User {
private int birthYear;
private String fullName;
public User() {}
public User(String fullName, int birthYear) {
this.fullName = fullName;
this.birthYear = birthYear;
}
public long getBirthYear() {
return birthYear;
}
public String getFullName() {
return fullName;
}
}
但是在线 List<User> userList=dataSnapshot.getValue(t);
我收到类似
的错误
FATAL EXCEPTION: main
Process: com.chhota.firebasepractice, PID: 8233
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:210)
at
com.chhota.firebasepractice.MainActivity.onDataChange(MainActivity.java:40)
at
com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at
com.firebase.client.core.view.EventRaiser.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:733)
你在 the location you query 处有这个 JSON 结构:
{
"-KCdZSoPFJ91-VKCA4pq" : {
"birthYear" : 1912,
"fullName" : "Alan Turing"
},
"-KCdZSoeKEYo96gb2GBF" : {
"birthYear" : 1913,
"fullName" : "Mohmad"
}
}
请下次将此添加到您的问题中,以便人们有足够的信息继续。
由于您使用的是 ValueEventListener
,您将在 onDataChange()
回调中获得整个 JSON。这不是用户的 List,而是将键(-KCdZSoPFJ91-VKCA4pq
等)映射到用户的 Map。
所以:
GenericTypeIndicator<Map<String, User>> t=new GenericTypeIndicator<Map<String, User>>(){};
Map<String, User> userMap=dataSnapshot.getValue(t);
for (User user: userMap.values()) {
System.out.println(user.getBirthYear());
}
firebase=new Firebase("https://flickering-inferno-4404.firebaseio.com/User/awosome");
firebase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String temp=dataSnapshot.getValue().toString();
GenericTypeIndicator<List<User>> t=new GenericTypeIndicator<List<User>>(){};
List<User> userList=dataSnapshot.getValue(t);
if (userList!=null){
userList.get(0).getBirthYear();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
并且用户模型是
public class User {
private int birthYear;
private String fullName;
public User() {}
public User(String fullName, int birthYear) {
this.fullName = fullName;
this.birthYear = birthYear;
}
public long getBirthYear() {
return birthYear;
}
public String getFullName() {
return fullName;
}
}
但是在线 List<User> userList=dataSnapshot.getValue(t);
我收到类似
FATAL EXCEPTION: main Process: com.chhota.firebasepractice, PID: 8233 com.firebase.client.FirebaseException: Failed to bounce to type at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:210) at com.chhota.firebasepractice.MainActivity.onDataChange(MainActivity.java:40) at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56) at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45) at com.firebase.client.core.view.EventRaiser.run(EventRaiser.java:38) at android.os.Handler.handleCallback(Handler.java:733)
你在 the location you query 处有这个 JSON 结构:
{
"-KCdZSoPFJ91-VKCA4pq" : {
"birthYear" : 1912,
"fullName" : "Alan Turing"
},
"-KCdZSoeKEYo96gb2GBF" : {
"birthYear" : 1913,
"fullName" : "Mohmad"
}
}
请下次将此添加到您的问题中,以便人们有足够的信息继续。
由于您使用的是 ValueEventListener
,您将在 onDataChange()
回调中获得整个 JSON。这不是用户的 List,而是将键(-KCdZSoPFJ91-VKCA4pq
等)映射到用户的 Map。
所以:
GenericTypeIndicator<Map<String, User>> t=new GenericTypeIndicator<Map<String, User>>(){};
Map<String, User> userMap=dataSnapshot.getValue(t);
for (User user: userMap.values()) {
System.out.println(user.getBirthYear());
}