我希望向用户集合中的所有用户添加一个整数数组字段。
我发现要执行此操作,最好使用List而不是数组。 (Writing array in Firebase android)。
将这个integar数组添加到我已经在运行的User Registration活动中后,它可以完美地上传到数据库。但是随后登录时出现问题。产生以下错误:
java.lang.RuntimeException:无法反序列化对象。类java.util.List具有泛型类型参数,请改用GenericTypeIndicator(在“站点”中找到)
登录活动中代码的这一部分:
userRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
Log.d(TAG, "onComplete: successfully set the user client." + task.getResult());
User user = task.getResult().toObject(User.class);
((UserClient)(getApplicationContext())).setUser(user);
}
}
});
具体在这一点上:
User user = task.getResult().toObject(User.class);
这是我的User.java:
public class User implements Parcelable{
private String email;
private String user_id;
private String username;
private String avatar;
private List stops;
private ClassLoader classLoader;
public User(String email, String user_id, String username, String avatar, List stops, ClassLoader classLoader) {
this.email = email;
this.user_id = user_id;
this.username = username;
this.avatar = avatar;
this.stops = stops;
this.classLoader = classLoader;
}
public List getStops() {
return stops;
}
public void setStops(List stops) {
this.stops = stops;
}
public ClassLoader getClassLoader() {
return classLoader;
}
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
public User() {
}
protected User(Parcel in) {
email = in.readString();
user_id = in.readString();
username = in.readString();
avatar = in.readString();
in.readList(stops, classLoader);
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public static Creator<User> getCREATOR() {
return CREATOR;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"email='" + email + '\'' +
", user_id='" + user_id + '\'' +
", username='" + username + '\'' +
", avatar='" + avatar + '\'' +
", stops='" + stops + '\'' +
'}';
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(email);
dest.writeString(user_id);
dest.writeString(username);
dest.writeString(avatar);
dest.writeList(stops);
}
数据库如下
收藏>用户
文档> erEq5iWy1Fd1Jaj3wl2AeMOlXp62
Fields>化身:null,电子邮件“ bob @ gmail.com,classLoader:null,停止:[0,0,0,0,0,0,0]”,user_id:“ erEq5iWy1Fd1Jaj3wl2AeMOlXp62”,用户名:“ bob” < / p>
答案 0 :(得分:1)
您需要为列表提供一个通用类型。
List<Integer> stops;
您的模型类示例
public class User implements Parcelable{
private String email;
private String user_id;
private String username;
private String avatar;
private List<Integer> stops;
private ClassLoader classLoader;
public User(String email, String user_id, String username, String avatar, List<Integer> stops, ClassLoader classLoader) {
this.email = email;
this.user_id = user_id;
this.username = username;
this.avatar = avatar;
this.stops = stops;
this.classLoader = classLoader;
}
public List<Integer> getStops() {
return stops;
}
public void setStops(List<Integer> stops) {
this.stops = stops;
}
}