ArrayAdapter and ListView bug



我在通过带有ArrayAdapter的ListView显示一些数据时遇到了一个错误。

我的数据中有由其 ID 定义的订单实例。 列表视图通过带有 ID 的文本视图显示它。 问题是第一个(并且总是第一个(实例的 ID 写入了 3 次。 前任:

订购编号111 订购 n°2 订单 n°3

这不是我第一次使用 ListView 和适配器,我从未有过这样的分歧。

public class OrderAdapter extends ArrayAdapter<Order> {
public OrderAdapter(Context context, ArrayList<Order> orders) {
super(context, 0, orders);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Order order = getItem(position);
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.order, parent, false);
}
TextView orderCheckedTextView = (TextView)convertView.findViewById(R.id.orderCheckedTextView);
System.out.println("@@@"+order.getId());
orderCheckedTextView.setText(orderCheckedTextView.getText()+Integer.toString(order.getId()));
return convertView;
}}

打印返回:

@@@1 @@@1 @@@1 @@@2

然后我创建 3 个订单将其添加到数据集并将适配器设置为 listView:

Order order1 = new Order(Type.LIVRAISON,State.PREPARATION, PaymentType.CB, articleList, null);
Order order2 = new Order(Type.EMPORTE,State.PREPARATION, PaymentType.TICKET, articleList, null);
Order order3 = new Order(Type.EMPORTE,State.PREPARATION, PaymentType.TICKET, articleList, null);
parentActivity.model.getOrderlist().addOrder(order1);
parentActivity.model.getOrderlist().addOrder(order2);
parentActivity.model.getOrderlist().addOrder(order3);
this.inProgressOrderAdapter = new OrderAdapter(parentActivity.getApplicationContext(), parentActivity.model.getOrderlist().getInProgressOrderList());
final ListView inProgressOrderListView = (ListView)parentActivity.findViewById(R.id.inProgressOrderListView);
inProgressOrderListView.setAdapter(inProgressOrderAdapter);

public class Order {
private OrderList orderList= null;
private static int staticID = 0;
private int id = 0;
private Type type = null;
private PaymentType paymentType = null;
private State state = null;
private Date time = null;
private ArrayList<Object> articleList = new ArrayList<>();
private double totalPrice = 0;
private Deliver deliver = null;
public Order(Type type, State state, PaymentType paymentType, ArrayList<Object> articleList, Deliver deliver) {
staticID++;
this.id = staticID;
this.type = type;
this.state = state;
this.paymentType = paymentType;
this.articleList = articleList;
this.deliver = deliver;
for(Object article:articleList){
if(article instanceof Article){
totalPrice += ((Article)article).getPrice();
}
else if(article instanceof Menu){
totalPrice += ((Menu)article).getPrice();
}
}
}
public OrderList getOrderList() {
return orderList;
}
public void setOrderList(OrderList orderList) {
this.orderList = orderList;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
if(state.equals(State.LIVRE) || state.equals(State.PREPARE))
this.orderList.switchOrderList(this);
}
public PaymentType getPaymentType() {
return paymentType;
}
public void setPaymentType(PaymentType paymentType) {
this.paymentType = paymentType;
}
public Date getTime() {
return this.time;
}
public ArrayList<Object> getArticleList() {
return articleList;
}
public void setArticleList(ArrayList<Object> articleList) {
this.articleList = articleList;
}
public void addArticle(Object article){
if(article instanceof Article || article instanceof Menu){
Boolean removed = this.articleList.add(article);
if(article instanceof Article && removed){
this.totalPrice += ((Article) article).getPrice();
}
else if(article instanceof Menu && removed){
this.totalPrice += ((Menu) article).getPrice();
}
}
}
public void removeArticle(Object article){
if(article instanceof Article || article instanceof Menu){
Boolean removed = this.articleList.remove(article);
if(article instanceof Article && removed){
this.totalPrice -= ((Article) article).getPrice();
}
else if(article instanceof Menu && removed){
this.totalPrice -= ((Menu) article).getPrice();
}
}
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public Deliver getDeliver() {
return deliver;
}
public void setDeliver(Deliver deliver) {
this.deliver = deliver;
}}
public enum PaymentType {
INTERNET, CB, ESPECE, TICKET;}

谢谢

问题出在您声明私有静态 int staticID = 0的行上;对于订单类的每个对象创建,订单实例是使用其所有成员变量和静态变量创建的,因此对于每个订单对象,在创建对象时此 staticId = 0。假设您创建的第一个对象 Order1 这里 staticId = 0;在 custructor 中,您将其增加到 1,因此对于 order1 id= 1

2-现在对于再次创建的order2对象,此实例具有staticId值= 0而不是1(如果您认为我在创建一阶时将其增加到1,那么您将静态视为错误的理解bcs,当新对象再次创建其所有成员变量时(因此对于order2对象构造函数,您将其递增为0到1,因此对于order2 id再次设置为1而不是2。

3- 订单 3 继续与订单 2 相同。

现在的解决方案是在创建对象时在构造函数中传递您的 id 值,如下所示:

Order order1 = new Order(1,Type.LIVRAISON,State.PREPARATION, PaymentType.CB, articleList, null);
Order order2 = new Order(2,Type.EMPORTE,State.PREPARATION, PaymentType.TICKET, articleList, null);
Order order3 = new Order(3,Type.EMPORTE,State.PREPARATION, PaymentType.TICKET, articleList, null);
parentActivity.model.getOrderlist().addOrder(order1);
parentActivity.model.getOrderlist().addOrder(order2);
parentActivity.model.getOrderlist().addOrder(order3);

并在订单构造函数中增加一个参数:

public Order(int **orderid**,Type type, State state, PaymentType paymentType, ArrayList<Object> articleList, Deliver deliver) {
staticID++; **// dont use this staticId**
this.id = orderid;
this.type = type;
this.state = state;
this.paymentType = paymentType;
this.articleList = articleList;
this.deliver = deliver;
for(Object article:articleList){
if(article instanceof Article){
totalPrice += ((Article)article).getPrice();
}
else if(article instanceof Menu){
totalPrice += ((Menu)article).getPrice();
}
}
}

最新更新