我有一个实现Parcelable interface
的包装类。我使用这个包装器在活动之间传递ArrayList
。它工作正常,但在另一个活动中检索int
时,一个0
字段始终提供ArrayList
。请在下面找到我的代码。我究竟做错了什么?是否有任何尺寸限制?
import android.os.Parcel;
import android.os.Parcelable;
public class ModelDeviceDetails implements Parcelable{
String responseCode;
String nickName;
float latitude;
float longitude;
String deviceName;
String uniqueId;
String batLevel;
int usedChaseModes;
int plannedChaseModes;
int usedLocateNow;
int plannedLocateNow;
public ModelDeviceDetails() {}
public ModelDeviceDetails(Parcel source) {
deviceName = source.readString();
uniqueId = source.readString();
nickName = source.readString();
latitude = source.readFloat();
longitude = source.readFloat();
batLevel = source.readString();
usedChaseModes = source.readInt();
plannedChaseModes = source.readInt();
usedLocateNow = source.readInt();
plannedLocateNow = source.readInt();
responseCode = source.readString();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.deviceName);
dest.writeString(this.uniqueId);
dest.writeString(this.nickName);
dest.writeFloat(this.latitude);
dest.writeFloat(this.longitude);
dest.writeString(this.batLevel);
dest.writeInt(this.usedChaseModes);
dest.writeInt(this.usedLocateNow);
dest.writeInt(this.plannedChaseModes);
dest.writeInt(this.plannedLocateNow);
dest.writeString(this.responseCode);
}
public static final Parcelable.Creator<ModelDeviceDetails> CREATOR = new Creator<ModelDeviceDetails>() {
@Override
public ModelDeviceDetails[] newArray(int size) {
return new ModelDeviceDetails[size];
}
@Override
public ModelDeviceDetails createFromParcel(Parcel source) {
return new ModelDeviceDetails(source);
}
};
}
我在主要活动中传递了ArrayList
,如下所示。
listViewActivity.putParcelableArrayListExtra("devices",devices);
检索我的listViewActivity
,
deviceList = intent.getParcelableArrayListExtra("devices");
我在发送列表之前检查了所有字段都正确填充。在listViewActivity
其他字段也正确。但是只有一个字段总是提供0
(我猜它会给int
提供默认值)。请建议任何其他方法。
答案 0 :(得分:1)
public ModelDeviceDetails(Parcel source) {
deviceName = source.readString();
uniqueId = source.readString();
nickName = source.readString();
latitude = source.readFloat();
longitude = source.readFloat();
batLevel = source.readString();
usedChaseModes = source.readInt();
plannedChaseModes = source.readInt(); // <-------- this
usedLocateNow = source.readInt(); // <-------- and this
plannedLocateNow = source.readInt();
responseCode = source.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.deviceName);
dest.writeString(this.uniqueId);
dest.writeString(this.nickName);
dest.writeFloat(this.latitude);
dest.writeFloat(this.longitude);
dest.writeString(this.batLevel);
dest.writeInt(this.usedChaseModes);
dest.writeInt(this.usedLocateNow);
dest.writeInt(this.plannedChaseModes);
dest.writeInt(this.plannedLocateNow);
dest.writeString(this.responseCode);
}
如果你在你的班级中实施Parcelable,那么你可以发送:
ArrayList<ModelDeviceDetails> list = new ArrayList<ModelDeviceDetails>();
list.add(new ModelDeviceDetails(...));
list.add(new ModelDeviceDetails(...));
Intent mySendingActivity = new Intent(getApplicationContext(), MySendingActivity.class);
mySendingActivity.putParcelableArrayListExtra("details", list);
并参与您的活动:
Bundle b = getIntent().getExtras();
ArrayList<ModelDeviceDetails> l = b.getParcelableArrayList("details");
Log.i("MySendingActivity", "Details:" + l);
答案 1 :(得分:0)
我找不到解决此问题的方法,并使用Serializable
代替Parcelable
进行管理
包装类
import java.io.Serializable;
public class DeviceDetails implements Serializable {
private static final long serialVersionUID = 1L;
public DeviceDetails(){
}
String responseCode;
String nickName;
float latitude;
float longitude;
String deviceName;
String uniqueId;
String batLevel;
int usedChaseModes;
int plannedChaseModes;
int usedLocateNow;
int plannedLocateNow;
//getters and setters
}
发送信息
Intent intent = new Intent(getApplicationContext(), MySendingActivity.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable("myKey", deviceDetails);
intent.putExtras(mBundle);
startActivity(intent);
deviceDetails
以上的类型为ArrayList<DeviceDetails>
。
接收信息
ArrayList<DeviceDetails> deviceList = (ArrayList<DeviceDetails>) intent.getSerializableExtra("myKey");