在我发现的代码中,作者正在硬编码setOrderName和Status的值。
我将从Web服务中提取这些值,但它在这里看起来像我需要根据我拉的记录数量来增加o1,o2,o3对象。它是否正确?如果是这样,我将如何实现这一目标。
如果我喋喋不休,请随时询问具体细节。 提前致谢
private void getOrders(){
try{
m_orders = new ArrayList<Order>();
Order o1 = new Order();
o1.setOrderName("SF services");
o1.setOrderStatus("Pending");
Order o2 = new Order();
o2.setOrderName("SF Advertisement");
o2.setOrderStatus("Completed");
m_orders.add(o1);
m_orders.add(o2);
Thread.sleep(5000);
Log.i("ARRAY", ""+ m_orders.size());
} catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
runOnUiThread(returnRes);
}
public class Order {
private String orderName;
private String orderStatus;
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public String getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(String orderStatus) {
this.orderStatus = orderStatus;
}
}
答案 0 :(得分:0)
不需要多个Order
个变量。而不是:
m_orders = new ArrayList<Order>();
Order o1 = new Order();
o1.setOrderName("SF services");
o1.setOrderStatus("Pending");
Order o2 = new Order();
o2.setOrderName("SF Advertisement");
o2.setOrderStatus("Completed");
m_orders.add(o1);
m_orders.add(o2);
你可以写:
m_orders = new ArrayList<Order>();
Order o;
o = new Order();
o.setOrderName("SF services");
o.setOrderStatus("Pending");
m_orders.add(o);
o = new Order();
o.setOrderName("SF Advertisement");
o.setOrderStatus("Completed");
m_orders.add(o);
请注意,创建和添加订单的4行使用相同的局部变量o
。