简体   繁体   中英

Add boolean array as a field to Firestore document

I am looking to add a integar array field to all users in my user collection.

I found out to do this it is better to use a List than an array. ( Writing array in Firebase android ).

Upon Adding this integar array to my already working User Registration activity,it uploads to the database perfectly well. however it subsequently has problems logging in. producing the following error:

java.lang.RuntimeException: Could not deserialize object. Class java.util.List has generic type parameters, please use GenericTypeIndicator instead (found in field 'stops')

At this section of the code in the Login Activity:

 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);
                        }
                    }
                });

Specifically at this point:

    User user = task.getResult().toObject(User.class);

Here is my 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);
}

The database looks as follows

Collection > Users

Documents > erEq5iWy1Fd1Jaj3wl2AeMOlXp62

Fields> avatar: null, email "bob@gmail.com, classLoader: null, stops:[0,0,0,0,0,0]", user_id:"erEq5iWy1Fd1Jaj3wl2AeMOlXp62", username:"bob"

You need to provide a generic type to your List.

List<Integer> stops;

Your model class example

    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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM