1

I'm facing a bug while displaying some data through ListView with ArrayAdapter.

I have Order instances in my data defined by its ID. The ListView displays it through a TextView with the ID. The problem is that the first (and always the first) instance has its ID written 3 times. ex:

Order n°111 Order n°2 Order n°3

It's not my first experience with ListViews and adapters and I've never had such a disagreement.

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;
}}

Here the print returns:

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

Then I create 3 Orders add it to the data set and set the adapter to the 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;}

Thanks

OTmn
  • 165
  • 1
  • 2
  • 15

1 Answers1

0

Problem is with line where you declared your private static int staticID = 0; for each object creation of Order class and instance of order is created with all its memeber variable and static variable so for each order object this staticId = 0 at the time of object creation. Suppose your created first object Order1 here staticId = 0; and in custructor you incremented it to 1 so for order1 id= 1

2- Now for order2 Object created again this instance have staticId value = 0 not 1 (if you think i incremented it to 1 at the time of first order creation than you are taking static as wrong understanding bcs when ever a new object created all its member variable again created with it) so for order2 objects constructor you increment it 0 to 1 so for order2 id is again set to 1 not 2.

3- order3 continue same as order2.

Now solution is pass your id value in the constructor at the time of object creation like below:

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);

And increase one parameter in Order Constructor:

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();
        }
    }
}
Ashif Ahamad
  • 150
  • 1
  • 7