简体   繁体   English

在意图传递的同一包中使用putParcelableArrayList和putInt

[英]use putParcelableArrayList and putInt on the same bundle passed in intent

this error is driving me mad 这个错误使我发疯

this is the code to pass the parameters: 这是传递参数的代码:

Intent intent = new Intent(this, TheClass.class);
Bundle bundle = new Bundle();
bundle.putInt("int_value",1);
bundle.putParcelableArrayList("USER_ARR",arr);
intent.putExtras(bundle);
startActivity(intent);

arr is ArrayList and User is parcellable arr是ArrayList并且User是可拆分的

the other side is: 另一面是:

Bundle bundle = this.getIntent().getExtras();
ArrayList<User> users = bundle.getParcelableArrayList("USER_ARR");
int int_value = bundle.getInt("int_value");

I also tried following this link: Problems passing integer and serializable on bundle and do it without bundles, but still can't get it to work the integer is not passed, unless it is passed without the paracellable array list 我也尝试按照此链接进行操作: 在整数上传递整数和可序列化的问题,并且在没有捆绑的情况下进行操作,但仍然无法使整数正常工作,除非通过无可拆分数组列表,否则不会传递整数

Edit: 编辑:

the user class looks like this: 用户类如下所示:

public class User implements Comparable<User>, Parcelable{

private int user_id;
private String name;
private String status;
private String phone;
private byte[] picture;
private Timestamp last_login;
private boolean is_verified;

public User(int user_id) {
    this.setUserId(user_id);
}


public int getUserId() {
    return user_id;
}

public void setUserId(int user_id) {
    this.user_id = user_id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getPhone() {
    return phone;
}


public void setPhone(String phone) {
    this.phone = phone;
}

public byte[] getPicture() {
    return picture;
}

public void setPicture(byte[] picture) {
    this.picture = picture;
}


public Timestamp getLastLogin() {
    return last_login;
}


public void setLastLogin(Timestamp last_login) {
    this.last_login = last_login;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + user_id;
    return result;
}


@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    User other = (User) obj;
    if (user_id != other.user_id)
        return false;
    return true;
}


@Override
public int describeContents() {
    return 0;
}


public static final Parcelable.Creator<User> CREATOR =
        new Parcelable.Creator<User>(){

    @Override
    public User createFromParcel(Parcel source) {
        return new User(source);
    }

    @Override
    public User[] newArray(int size) {
        return new User[size];
    }
};
public User(Parcel source){
    readFromParcel(source);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(user_id);
    dest.writeString(name);
    dest.writeString(status);
    dest.writeString(phone);
    if (picture != null)
        dest.writeByteArray(picture);
    if (last_login == null)
        dest.writeString(null);
    else 
        dest.writeString(last_login.toString());
}

public void readFromParcel(Parcel source){
    user_id = source.readInt();
    name = source.readString();
    status = source.readString();
    phone = source.readString();
    if (picture != null)
        source.readByteArray(picture);
    String login = source.readString();
    if (login != null)
        last_login = Timestamp.valueOf(login);
    else 
        last_login = null;
}

public boolean contains(CharSequence str, Locale locale) {
    str = ((String)str).toLowerCase(locale);

    String name = this.getName();
    if (name != null)
        name = name.toLowerCase(locale);

    String phone = this.getPhone();
    if (phone != null)
        phone = phone.toLowerCase(locale);

    if (name != null && name.contains(str)) {
        return true;
    }
    if (phone != null && phone.contains(str)) {
        return true;
    }

    return false;
}


@Override
public int compareTo(User another) {
    String name1 = this.getName();
    String name2 = another.getName();

    return name1.compareTo(name2);
}


@Override
public String toString() {
    return "User [user_id=" + user_id + ", name=" + name + ", status="
            + status + ", phone=" + phone + ", last_login=" + last_login
            + ", is_verified=" + is_verified + "]";
}


public boolean getIsVerified() {
    return is_verified;
}


public void setIsVerified(boolean is_verified) {
    this.is_verified = is_verified;
}


}

Found out what had happened, it was related to a newer question I asked, which didn't have any answers either 发现发生了什么事,这与我问的一个新问题有关,该问题也没有任何答案

the question is: parcelable ArrayList passed partially with null elements 问题是:可打包的ArrayList部分传递了null元素

I was reading a byte array inside my parcelable only if the size of the byte array was bigger than zero, but not reading it did not advance the dataPosition() 仅当字节数组的大小大于零时,我才在我的可包裹体内读取一个字节数组,但不读取它并没有推进dataPosition()

thanks to this question Make custom Parcelable containing byte array I found the answer 感谢这个问题, 使包含字节数组的自定义Parcelable可以找到答案

have a great day everybody 祝大家有美好的一天

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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