在Java中动态创建ImageButtons从ArrayList到RelativeLayout

时间:2013-09-17 12:35:47

标签: java android dynamic arraylist imagebutton

我正在尝试从Android中的ArrayList动态创建ImageButtons。我已经尝试了多种方法来使这个工作,但有时有一个神秘的“按钮”不在列表中但反正出现。我正在使用相对布局,因为根据ArrayList中有多少项,按钮应该是不同的大小和不同的设计。任何帮助将不胜感激。

public void handlers() //throws Exception
{
    ArrayList<WarehouseProductItemExt> coffeeItemList = globals.GetCoffeeItems();
    for(final WarehouseProductItemExt wpi : coffeeItemList)
    {
        count ++;
        butt = new Button(this);
    butt.setBackgroundDrawable(Coffee);
        butt.setId(count);
        RelativeLayout.LayoutParams params = getLayout(count,    coffeeItemList.size());
        butt.setLayoutParams(params);
        butt.setText(wpi.name);
        layout.addView(butt);

        butt.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {


                //Handler handler = new Handler();
                //handler.postDelayed(new Runnable() {

                //public void run() 
                //{
                    //butt.setBackgroundDrawable(Coffee);
            //  }
                //}, 100); // 100ms delay
                double price = 0.0;
                for(WarehouseProductItemPricing wpip : wpi.prices)
                {
                    if(wpip.idPriceLevelType == 1)
                    {
                        price = wpip.price;

                    }
                }
                Boolean itemExistsInList = false;
                for(TableItems ti : tableItems)
                {
                    if(ti.getIdWarehouseProductItem() == wpi.idWarehouseProductItem)
                    {
                        itemExistsInList = true;
                        Globals globals = Globals.getInstance();
                        globals.addItemToCart(wpi);
                        ti.setQuantity(ti.getQuantity() + 1);
                    }
                }
                if(!itemExistsInList)
                {
                    TableItems n = new TableItems(wpi.name, 1, price, wpi.idWarehouseProductItem);
                    Globals globals = Globals.getInstance();
                    globals.addItemToCart(wpi);
                    tableItems.add(n);
                }
            //butt.setBackgroundDrawable(selectButt);
            Intent na = new Intent(".ShoppingCartActivity");
            startActivity(na);
            finish();
            }
        });
    }


private android.widget.RelativeLayout.LayoutParams getLayout(int count, int size) 
{
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(200, 360);
    switch(size)
            {
    case 1:
        params = new RelativeLayout.LayoutParams(200, 360);
        switch(count){
        case 1:
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            break;
        default:
            break;
        }
        break;
    case 2:
        params = new RelativeLayout.LayoutParams(200, 360);
        switch(count){
        case 1:
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            params.setMargins(200, 0, 200, 0);
            break;
        case 2:
            params.addRule(RelativeLayout.RIGHT_OF, 1);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            params.setMargins(200, 0, 200, 0);
            break;
        default:
            break;
        }
        break;
    case 3:
        params = new RelativeLayout.LayoutParams(200, 360);
        switch(count){
        case 1:
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.setMargins(200, 0, 0, 0);
            break;
        case 2:
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            params.setMargins(0, 0, 0, 0);
            break;
        case 3:
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.setMargins(0, 0, 200, 0);
            break;
        default:
            break;
        }
        break;
    case 4:
        params = new RelativeLayout.LayoutParams(200, 360);
        switch(count){
        case 1:
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.setMargins(50, 250, 0, 0);
            break;
        case 2:
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.addRule(RelativeLayout.RIGHT_OF, 1);
            params.setMargins(100, 0, 100, 250);
            break;
        case 3:
            params.addRule(RelativeLayout.RIGHT_OF, 2);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.setMargins(0, 250, 100, 0);
            break;
        case 4:
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.addRule(RelativeLayout.RIGHT_OF, 3);
            params.setMargins(0, 0, 50, 250);
            break;
        default:
            break;
        }
        break;
    case 5:
        params = new RelativeLayout.LayoutParams(170, 306);
        switch(count){
        case 1:
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.setMargins(50, 0, 0, 50);
            break;
        case 2:
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.addRule(RelativeLayout.RIGHT_OF, 1);
            params.setMargins(50, 50, 0, 0);
            break;
        case 3:
            params.addRule(RelativeLayout.RIGHT_OF, 2);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.setMargins(50, 0, 0, 50);
            break;
        case 4:
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.addRule(RelativeLayout.RIGHT_OF, 3);
            params.setMargins(50, 50, 0, 0);
            break;
        case 5:
            params.addRule(RelativeLayout.RIGHT_OF, 4);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.setMargins(50, 0, 0, 50);
            break;
        default:
            break;
        }
        break;
    case 6:
        params = new RelativeLayout.LayoutParams(200, 360);
        switch(count){
        case 1:
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.setMargins(0, 0, 0, 0);
            break;
        case 2:
            params.addRule(RelativeLayout.BELOW, 1);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.setMargins(0, 0, 0, 0);
            break;
        case 3:
            params.addRule(RelativeLayout.RIGHT_OF, 2);
            params.addRule(RelativeLayout.BELOW, 1);
            params.setMargins(100, 0, 100, 0);
            break;
        case 4:
            params.addRule(RelativeLayout.BELOW, 1);
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            params.setMargins(0, 0, 0, 0);
            break;
        case 5:
            params.addRule(RelativeLayout.BELOW, 1);
            params.addRule(RelativeLayout.RIGHT_OF, 4);
            params.setMargins(100, 0, 100, 0);
            break;
        case 6:
            params.addRule(RelativeLayout.BELOW, 1);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.setMargins(0, 0, 0, 0);
            break;
        default:
            break;
        }
        break;
    case 7:
        params = new RelativeLayout.LayoutParams(125, 225);
        switch(count){
        case 1:
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.setMargins(50, 0, 0, 0);
            break;
        case 2:
            params.addRule(RelativeLayout.RIGHT_OF, 1);
            params.addRule(RelativeLayout.ABOVE, 1);
            params.setMargins(50, 0, 0, 0);
            break;
        case 3:
            params.addRule(RelativeLayout.RIGHT_OF, 2);
            params.addRule(RelativeLayout.ABOVE, 2);
            params.setMargins(50, 0, 0, 0);
            break;
        case 4:
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            params.setMargins(0, 0, 0, 0);
            break;
        case 5:
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.setMargins(0, 0, 50, 0);
            break;
        case 6:
            params.addRule(RelativeLayout.BELOW, 7);
            params.addRule(RelativeLayout.LEFT_OF, 7);
            params.setMargins(0, 0, 50, 0);
            break;
        case 7:
            params.addRule(RelativeLayout.BELOW, 6);
            params.addRule(RelativeLayout.LEFT_OF, 6);
            params.setMargins(0, 0, 50, 0);
            break;
        default:
            break;
        }
        break;
    case 8:
        params = new RelativeLayout.LayoutParams(180, 144);
        switch(count){
        case 1:
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.width = 180;
            params.height = 324;
            params.setMargins(200, 50, 20, 0);
            break;
        case 2:
            params.addRule(RelativeLayout.RIGHT_OF, 1);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.width = 180;
            params.height = 324;
            params.setMargins(20, 50, 20, 0);
            break;
        case 3:
            params.addRule(RelativeLayout.RIGHT_OF, 2);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.width = 180;
            params.height = 324;;
            params.setMargins(20, 50, 20, 0);
            break;
        case 4:
            params.addRule(RelativeLayout.RIGHT_OF, 3);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.width = 180;
            params.height = 324;

            params.setMargins(20, 50, 20, 0);
        case 5:
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.width = 180;
            params.height = 324;
            params.setMargins(20, 0, 100, 0);
            break;
        case 6:
            params.addRule(RelativeLayout.LEFT_OF, 5);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.width = 180;
            params.height = 324;
            params.setMargins(20, 0, 20, 0);
            break;
        case 7:
            params.addRule(RelativeLayout.LEFT_OF, 6);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.width = 180;
            params.height = 324;
            params.setMargins(20, 0, 20, 0);
            break;
        case 8:
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.width = 180;
            params.height = 324;
            params.addRule(RelativeLayout.LEFT_OF, 7);
            params.setMargins(0, 0, 20, 0);
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
return params;

}

1 个答案:

答案 0 :(得分:0)

所以,我认为问题可能是切换案例的格式化问题? (绝对是一个菜鸟的错误)但经过开关盒慢慢调试后我已经解决了问题。以下是从arrayList动态创建8个ImageButtons的开关案例的代码。

    case 8:
        params = new RelativeLayout.LayoutParams(180, 144);
        switch(count){
        case 1:

            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.width = 180;
            params.height = 324;
            params.setMargins(300, 40, 30, 0);
            break;
        case 2:
            params.addRule(RelativeLayout.RIGHT_OF, 1);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.width = 180;
            params.height = 324;
            params.setMargins(20, 40, 30, 0);
            break;
        case 3:
            params.width = 180;
            params.height = 324;
            params.addRule(RelativeLayout.RIGHT_OF, 2);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.setMargins(0, 40, 30, 0);
            break;
        case 4:
            params.width = 180;
            params.height = 324;
            params.addRule(RelativeLayout.RIGHT_OF, 3);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.setMargins(0, 40, 30, 0);
            break;
        case 5:
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.width = 180;
            params.height = 324;
            params.setMargins(40, 0, 300, 0);
            break;
        case 6:
            params.addRule(RelativeLayout.LEFT_OF, 5);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.height = 324;
            params.width = 180;
            params.setMargins(40, 0, 0, 0);
            break;
        case 7:
            params.addRule(RelativeLayout.LEFT_OF, 6);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.width = 180;
            params.height = 324;
            params.setMargins(40, 0, 0, 0);
            break;
        case 8:
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.width = 180;
            params.height = 324;
            params.addRule(RelativeLayout.LEFT_OF, 7);
            break;
        default:
            break;
        }
        break;
    default:
        break;