getView方法无法正常工作 - 添加额外的空行

时间:2013-03-15 21:16:07

标签: android listview android-arrayadapter

我正在尝试构建一个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;

}

1 个答案:

答案 0 :(得分:0)

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

例如,如果getCount()返回5,您将在listview上获得5行。因此,请检查您的getCount(),您可以覆盖它以使其返回正确的行数。