Android显示出对象,android – 通过IPC将对象列表从服务传递到...

在开发一个Android应用时,作者遇到了一个问题,即从非本地服务获取的Order对象列表在传递到活动时,只有第二个元素是空值。尽管服务端返回的列表是正确的,但在活动中的数据显示不正常。问题可能与Order类的复杂性有关,因为它包含自定义对象属性,而之前工作的Customer类则没有。已经尝试为Order类实现了Parcelable接口,但问题仍然存在。
摘要由CSDN通过智能技术生成

我正在编写一个运行非本地服务的Android应用程序,以便从互联网上获取数据.我有一个绑定到所述服务的活动,并检索Order对象列表以显示它们.

将列表从服务传递到活动时似乎出现了问题.虽然这对我来说似乎很简单.

The Problem

All the items in the list, but the item at index 1, are null values.

调试显示服务函数具有正确的列表并返回正确的列表:

这是返回列表的服务中的函数:

public List getOrders() throws RemoteException {

synchronized(service.orders) {

// service.orders returns a hashmap

// where the key is the ID of the order

// The function returns a simple list, so we parse to an ArrayList

List result = new ArrayList(service.orders.values());

return result;

}

}

这是Activity中调用服务并检索列表的函数(其中api是绑定到服务的结果):

handler.post(new Runnable() {

public void run() {

try {

List result = api.getOrders();

displayOrders(result);

} catch (Throwable t) {

Log.e(TAG, "Error while updating the UI with orders", t);

}

}

});

这让我感到非常难过,因为我还有一个活动来查看客户和一个返回客户列表的服务功能,并且运行完美.两者之间的主要区别在于Customer没有任何自定义对象属性,而Order有一些.

编辑:为Order类添加了Parcelable实现(通过删除大多数原始属性简化):

/*

* Parcelabe interface implementation

*/

public static final Creator CREATOR = new Creator() {

public Order createFromParcel(Parcel source) {

return new Order(source);

}

public Order[] newArray(int size) {

return new Order[size];

}

};

public Order(Parcel source) {

ID = source.readInt();

OrderID = source.readInt();

CustomerID = source.readInt();

TotalAmount = source.readDouble();

TotalProducts = source.readDouble();

OrderDate = source.readString();

InvoiceDate = source.readString();

DeliveryDate = source.readString();

// Custom objects

Customer = source.readParcelable(Customer.class.getClassLoader());

History = new ArrayList();

source.readTypedList(History, OrderHistory.CREATOR);

State = source.readParcelable(OrderState.class.getClassLoader());

}

public int describeContents() {

return Order.class.hashCode();

}

public void writeToParcel(Parcel dest, int flags) {

dest.writeInt(ID);

dest.writeInt(OrderID);

dest.writeInt(CustomerID);

dest.writeDouble(TotalAmount);

dest.writeDouble(TotalProducts);

dest.writeString(OrderDate);

dest.writeString(InvoiceDate);

dest.writeString(DeliveryDate);

// Custom object

dest.writeParcelable(Customer, flags);

dest.writeParcelable(State, flags);

dest.writeTypedList(History);

}

编辑:为OrderList类添加了代码:

public class OrderList extends ArrayList实现Parcelable {

/**

*

*/

private static final long serialVersionUID = 417326483896340670L;

public OrderList() {

}

public OrderList(Parcel in) {

readFromParcel(in);

}

/*

* Parcelabe interface implementation

*/

public OrderList(Collection values) {

this.addAll(values);

}

@SuppressWarnings("unchecked")

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

public OrderList createFromParcel(Parcel in) {

return new OrderList(in);

}

public OrderList[] newArray(int arg0) {

return null;

}

};

private void readFromParcel(Parcel in) {

this.clear();

//First we have to read the list size

int size = in.readInt();

//Reading remember that we wrote first the Name and later the Phone Number.

//Order is fundamental

for (int i = 0; i < size; i++) {

Order o = new Order();

o = in.readParcelable(Order.class.getClassLoader());

this.add(o);

}

}

public int describeContents() {

return 0;

}

public void writeToParcel(Parcel dest, int flags) {

int size = this.size();

// We have to write the list size, we need him recreating the list

dest.writeInt(size);

// We decided arbitrarily to write first the Name and later the Phone Number.

for (int i = 0; i < size; i++) {

Order o = this.get(i);

dest.writeParcelable(o, flags);

}

}

}

有什么指针吗?

如果您需要,请随时询问具体信息!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值