0

我正在尝试构建一个ArrayAdapter将输出一行和该行中项目的数量的。但是,每次添加额外的行时,我都会得到不想要的效果。

我不知道我的逻辑有什么问题,但我确信这是小问题。下面是我的getView()方法:

    public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(v == null){
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.order_row, null);
    }

    item = dishes.get(position);
    if(lastItem != null){
    if(!lastItem.getName().equals(item.getName())){

    dishName = (TextView)v.findViewById(R.id.dishName);
    dishPrice = (TextView)v.findViewById(R.id.dishPrice);

    dishName.setText(item.getName());
    dishPrice.setText(String.valueOf(item.getPrice()));

    dishQty = (TextView)v.findViewById(R.id.dishQty);
    dishQty.setText(String.valueOf(item.getQty()));
        }
    }
    else{
        dishName = (TextView)v.findViewById(R.id.dishName);
        dishPrice = (TextView)v.findViewById(R.id.dishPrice);

        dishName.setText(item.getName());
        dishPrice.setText(String.valueOf(item.getPrice()));

        dishQty = (TextView)v.findViewById(R.id.dishQty);
        dishQty.setText(String.valueOf(item.getQty()));

    }

    lastItem = item;
    return v;

}

目前我的输出是这样的

Chicken     QUANTITY
2.99            2

然后,如果我添加一个新项目,数量会按预期增加,但它会添加另一行:

DISH NAME     QUANTITY
DISH PRICE        0

关于我做错了什么的任何想法?

列表中的每个项目都是MenuItem我增加数量的类型,如下所示:

public void incQty(){
    qty++;
}

这是数量增加的MenuItem阵列适配器getView()NumberPicker

public View getView(final int position, View convertView, ViewGroup parent) {

    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.menuitem_row, null);
        v.setFocusable(true);
    }

    // assign values to view
    final MenuItem item = this.menuItems.get(position);

    TextView nameView = (TextView) v.findViewById(R.id.item_name);
    final TextView priceView = (TextView) v.findViewById(R.id.item_price);

    nameView.setText(item.getName() + " ");
    priceView.setText("€" + String.valueOf(item.getPrice()));

    // number picker
    np = (NumberPicker) v.findViewById(R.id.numpick);
    np.setMaxValue(20);
    np.setMinValue(0);
    np.setValue(0);
    np.setFocusable(false);
    // calculation occurs when values are changed...
    np.setOnValueChangedListener(new OnValueChangeListener() {
        public void onValueChange(NumberPicker picker, int oldVal,
                int newVal) {
            //Add to order - increment price
            if (newVal > oldVal) {
                total = (item.getPrice() * newVal) - item.getPrice()
                        * oldVal;
                Order.getInstance().addToOrder(item);
                item.incQty();
                amount += total;
                Order.getInstance().setAmount(amount);
                Toast.makeText(picker.getContext(), "Item Added: " + item.getName() + " Size: " + Order.getInstance().getOrderItems().size(), Toast.LENGTH_SHORT).show();
                confirmBtn.setVisibility(View.VISIBLE);


            }
            //remove from order - decrement price
            if (newVal < oldVal) {
                total = (item.getPrice() * oldVal) - item.getPrice()
                        * newVal;
                Order.getInstance().removeFromOrder(position);
                item.decQty();
                amount -= total;
                Order.getInstance().setAmount(amount);
                Toast.makeText(picker.getContext(), "Item removed: " + item.getName() + " Size: " + Order.getInstance().getOrderItems().size(), Toast.LENGTH_SHORT).show();
                if(Order.getInstance().getOrderItems().size() == 0){
                    confirmBtn.setVisibility(View.INVISIBLE);
                }

            }


            //update the text view price.
            price.setText("€" + String.valueOf(Order.getInstance().getAmount()));
        }
    });

    return v;

}
4

1 回答 1

0

您必须检查适配器的 getCount() 方法。getCount() 方法是确定从 getView() 方法创建多少行的方法。

例如,如果 getCount() 返回 5,您将在列表视图中获得 5 行。因此,请检查您的 getCount() 您可能会覆盖它以使其返回正确的行数。

于 2013-03-16T14:04:58.450 回答