我有2个对象,可以在我的应用程序(用户和帐户)中的不同活动之间共享。它们都实现了Parcelable。用户存储了一个Account对象数组,称为userAccounts,该对象旨在在活动之间转移,但是该数组并未在活动之间转移,即使它已在源中填充,它也带有0个对象活动(我已经验证了)。其他用户变量正在正确传输,因此我知道在Parcelable实现中这可能不是问题。
我尝试实现以下Parcelable方法
写方法
writeArray(); writeTypedArray();
读取方法
readArray(); readTypedArray();
除此之外,我还验证了是否正在填充Array并且正确访问了对象中的getter和setter方法。下面是对象User和Account实现的代码
public class Account implements Parcelable {
private String type;
private String name;
private int amount;
public Account(String type, String name, int amount){
this.type=type;
this.name=name;
this.amount=amount;
}
public String getType(){
return type;
}
public String getName(){
return name;
}
public int getAmount(){
return amount;
}
//Parcelable methods
public void writeToParcel(Parcel dest, int flags){
dest.writeString(type);
dest.writeString(name);
dest.writeInt(amount);
}
//Parcelable constructor
public Account(Parcel parcel){
type=parcel.readString();
name=parcel.readString();
amount=parcel.readInt();
}
public static final Parcelable.Creator<Account> CREATOR= new Parcelable.Creator<Account>(){
@Override
public Account createFromParcel(Parcel parcel){
return new Account(parcel);
}
@Override
public Account [] newArray(int size){
return new Account[size];
}
};
public int describeContents(){
return hashCode();
}
}
public class User implements Parcelable{
private String email_address;
private String user_name;
private String password;
private int totalIncome=0;
private int balance=0;
private int totalExpense=0;
private ArrayList<Account> userAccounts = new ArrayList<>();
private Account[] accountArray=new Account[userAccounts.size()];
public User(String email_address,String user_name,String password){
this.email_address=email_address;
this.user_name=user_name;
this.password=password;
}
//method for the user to create accounts
public void createAccount(String type,String name, int amount){
Account new_account= new Account(type,name,amount);
userAccounts.add(new_account);
if (new_account.getType().equals("Income")){
totalIncome=totalIncome+new_account.getAmount();
}
else if (new_account.getType().equals("Expense")){
totalExpense=totalExpense+new_account.getAmount();
}
}
//Calculates the balance of all the accounts
public int calculateBalance (){
balance=totalIncome-totalExpense;
return balance;
}
//getters
public String getTotalIncome(){
return Integer.toString(totalIncome);
}
public String getTotalExpense(){
return Integer.toString(totalExpense);
}
public String getBalance(){
return Integer.toString(balance);
}
public String getEmailAddress(){
return email_address;
}
public String getUserName(){
return user_name;
}
public String getPassword(){
return password;
}
public Account[] getAccounts(){
accountArray=this.convertToArray();
System.out.println("Accounts accesed");
System.out.println("accountArray size "+accountArray.length);
return accountArray;
}
//setters
public void setTotalIncome(int totalIncome){
this.totalIncome=totalIncome;
}
public void setTotalExpense(int totalExpense){
this.totalExpense=totalExpense;
}
public void setAccounts(Account[] accounts){
this.userAccounts=new ArrayList<Account>(Arrays.asList(accounts));
}
public void setBalance(int balance){
this.balance=balance;
}
//Parcelable methods
//Converts userAccounts to Array for parcelable
public Account[] convertToArray(){
accountArray=userAccounts.toArray(new Account[userAccounts.size()]);
return accountArray;
}
//Write object values to parcel for storage
public void writeToParcel(Parcel parcel, int flags){
parcel.writeString(email_address);
parcel.writeString(user_name);
parcel.writeString(password);
parcel.writeInt(totalIncome);
parcel.writeInt(balance);
parcel.writeInt(totalExpense);
parcel.writeTypedArray(accountArray,0);
}
//Parcel constructor
public User(Parcel parcel){
email_address=parcel.readString();
user_name=parcel.readString();
password=parcel.readString();
totalIncome=parcel.readInt();
balance=parcel.readInt();
totalExpense=parcel.readInt();
accountArray=parcel.createTypedArray(Account.CREATOR);
}
//Parcel creator
public static final Parcelable.Creator<User> CREATOR=new Parcelable.Creator<User>(){
@Override
public User createFromParcel(Parcel parcel){
return new User(parcel);
}
@Override
public User[]newArray(int size){
return new User[0];
}
};
//Return hashcode of object
public int describeContents(){
return hashCode();
}
在两次活动之间进行操作时,数组从具有n个元素(与我添加的数量一样)变为0个元素,这表明在Parcelable传输期间accountArray数据丢失了。非常感谢您提出的任何想法