0

我用我的代码创建了列表线性布局

String[] name = {"Chicken", "Beef", "Milk" ,"Juice"};
    final Integer[] price = {10, 50, 5, 3};
    final Integer[] qty = {1, 2, 5, 5};

    listOrder.removeAllViews();
    LinearLayout.LayoutParams image = new LinearLayout.LayoutParams(Utility.getDip(175, CartActivity.this), Utility.getDip(70, CartActivity.this));
    LinearLayout.LayoutParams text = new LinearLayout.LayoutParams(Utility.getDip(140, CartActivity.this), Utility.getDip(80, CartActivity.this));
    LinearLayout.LayoutParams textQty = new LinearLayout.LayoutParams(Utility.getDip(100, CartActivity.this), Utility.getDip(80, CartActivity.this));
    LinearLayout.LayoutParams textPrice = new LinearLayout.LayoutParams(Utility.getDip(125, CartActivity.this), Utility.getDip(80, CartActivity.this));
    LinearLayout.LayoutParams textTotal = new LinearLayout.LayoutParams(Utility.getDip(130, CartActivity.this), Utility.getDip(80, CartActivity.this));
    LinearLayout.LayoutParams editImage = new LinearLayout.LayoutParams(Utility.getDip(40, CartActivity.this), Utility.getDip(40, CartActivity.this));
    for(int i = 0; i < 4; i++){
        list = new LinearLayout(ctx);
        list.setOrientation(LinearLayout.HORIZONTAL);
        //if((i % 2) == 0) list.setBackgroundColor(R.color.bg_grey);

        listImage = new ImageView(ctx);
        //listImage.setImageDrawable(getResources().getDrawable(R.drawable.sample_logo_publisher));
        listImage.setBackgroundColor(getResources().getColor(R.color.bg_grey));
        listImage.setAdjustViewBounds(Boolean.TRUE);
        listImage.setScaleType(ScaleType.FIT_XY);

        image.gravity = Gravity.CENTER;
        image.setMargins(Utility.getDip(10, CartActivity.this), 0, Utility.getDip(10, CartActivity.this), 0);
        list.addView(listImage, image);

        listText = new TextView(ctx);
        listText.setText(name[i]);
        listText.setGravity(Gravity.CENTER);
        text.gravity = Gravity.CENTER;
        text.setMargins(Utility.getDip(10, CartActivity.this), 0, 0, 0);
        list.addView(listText, text);

        listQty = new TextView(ctx);
        listQty.setText(qty[i].toString());
        listQty.setGravity(Gravity.CENTER);
        textQty.gravity = Gravity.CENTER;
        textQty.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(5, CartActivity.this), 0);
        list.addView(listQty, textQty);

        listPrice = new TextView(ctx);
        listPrice.setText(price[i].toString());
        listPrice.setGravity(Gravity.CENTER_VERTICAL);
        textPrice.gravity = Gravity.CENTER;
        list.addView(listPrice, textPrice);

        Integer total = qty[i]*price[i];
        listTotal = new TextView(ctx);
        listTotal.setText(total.toString());
        listTotal.setGravity(Gravity.CENTER_VERTICAL);
        textTotal.gravity = Gravity.CENTER;
        textTotal.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(2, CartActivity.this), 0);
        list.addView(listTotal, textTotal);

        plus = new ImageView(ctx);
        plus.setImageDrawable(getResources().getDrawable(R.drawable.plus));
        plus.setAdjustViewBounds(Boolean.TRUE);
        plus.setScaleType(ScaleType.FIT_XY);
        editImage.gravity = Gravity.CENTER;
        list.addView(plus, editImage);
        final int pos = i;
        plus.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                qty[pos] = qty[pos] + 1;
                listQty.setText(qty[pos].toString());
                listTotal.setText(""+ (qty[pos]*price[pos]));
            }
        });

        minus = new ImageView(ctx);
        minus.setImageDrawable(getResources().getDrawable(R.drawable.min));
        minus.setAdjustViewBounds(Boolean.TRUE);
        minus.setScaleType(ScaleType.FIT_XY);
        editImage.gravity = Gravity.CENTER;
        list.addView(minus, editImage);

        remove = new ImageView(ctx);
        remove.setImageDrawable(getResources().getDrawable(R.drawable.trash));
        remove.setAdjustViewBounds(Boolean.TRUE);
        remove.setScaleType(ScaleType.FIT_XY);
        editImage.gravity = Gravity.CENTER;
        list.addView(remove, editImage);

        listOrder.addView(list);
    }

我想在点击加图片时添加数量。但是当我单击列表中的加号图像时,textQty 在列表末尾发生了变化,而不是在我单击图像的同一个列表中。

如何修复我的代码?

4

2 回答 2

0

您需要将 TextViews 和按钮链接在一起。他们不知道他们在 for 循环的同一迭代中。

当您通过循环时,您可以将每组按钮和文本视图放入一个数组中。

然后在您的 onClickListener 中,您将执行以下操作:

for(int i = 0; i < buttonArray.length; i++){
    if(v == buttonArray[i]){
       // make whatever changes to TextViews at index i
    }
}
于 2013-01-15T01:34:00.710 回答
0

我已经解决了我的问题。这是我的代码...
我在 listQty 和 listTotal 中设置了 id

listQty = new TextView(ctx);
listQty.setId(99000+i);
listQty.setText(qty[i].toString());
listQty.setGravity(Gravity.CENTER);
textQty.gravity = Gravity.CENTER;
textQty.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(5, CartActivity.this), 0);
list.addView(listQty, textQty);

listTotal = new TextView(ctx);
listTotal.setId(98000+i);
listTotal.setText(total.toString());
listTotal.setGravity(Gravity.CENTER_VERTICAL);
textTotal.gravity = Gravity.CENTER;
textTotal.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(2, CartActivity.this), 0);
list.addView(listTotal, textTotal);

并在 listQty 和 listTotal 的 clicklistener 设置变量中

plus = new ImageView(ctx);
plus.setImageDrawable(getResources().getDrawable(R.drawable.plus));
plus.setAdjustViewBounds(Boolean.TRUE);
plus.setScaleType(ScaleType.FIT_XY);
editImage.gravity = Gravity.CENTER;
list.addView(plus, editImage);
final int pos = i;
plus.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
        listQty = (TextView)findViewById(99000+pos);
        listTotal = (TextView)findViewById(98000+pos);
        qty[pos] = qty[pos] + 1;
        listQty.setText(qty[pos].toString());
        listTotal.setText(""+ (qty[pos]*price[pos]));
    }
});
于 2013-01-16T07:46:16.503 回答