无法动态添加视图

时间:2015-02-15 04:16:58

标签: android android-layout android-linearlayout

我在XML中有线性布局R.id.linearLayoutQuantityPrice。

我想在其中动态添加一个新的线性布局。

新的线性布局有两个textview和一个imageview。

List<Price> priceList = database.getItemQuantityPrice(id);
        LinearLayout linearLayoutQuantityPrice = (LinearLayout) rowView
                .findViewById(R.id.linearLayoutQuantityPrice);
        for (int i=0; i<priceList.size() ; i++) {
            Price price = new Price();
            price = priceList.get(i);
            LinearLayout newLinearLayout = new LinearLayout(context);
            newLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            LayoutParams newLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            newLinearLayout.setPadding(2, 2, 2, 2);
            newLinearLayout.setWeightSum(3);

            int iqpId = price.getIqpId();
            String quantityIn = price.getQuantityIn();
            int quantity = price.getItmQnt();
            int itemPrice = price.getItmPrc();

            TextView tvQuantity=new TextView(context);
            tvQuantity.setId(price.getIqpId()+10);
            tvQuantity.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1.0f));
            tvQuantity.setTextSize(12);
            if(quantityIn.equalsIgnoreCase("gm") && quantity>=1000){
                quantity = quantity/1000;
                quantityIn = new String("KG");
            }
            tvQuantity.setText(""+quantity+" "+quantityIn);
            newLinearLayout.addView(tvQuantity);

            TextView tvPrice=new TextView(context);
            tvPrice.setId(price.getIqpId()+11);
            tvPrice.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1.0f));
            tvPrice.setTextSize(12);
            tvPrice.setText("Rs. "+itemPrice);
            newLinearLayout.addView(tvPrice);

            ImageView ivButton = new ImageView(context);
            ivButton.setId(iqpId);
            ivButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1.0f));
            ivButton.setImageDrawable(rowView.getResources().getDrawable(R.drawable.add_button));
            ivButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int toCartId = v.getId();
                    Toast.makeText(context, "Added to cart" + toCartId, Toast.LENGTH_SHORT).show();
                }
            });
            linearLayoutQuantityPrice.addView(newLinearLayout);
        }

代码没有任何错误。但是没有添加所需的视图。

2 个答案:

答案 0 :(得分:0)

因为你忘了设置布局参数。

newLinearLayout.setLayoutParams(newLayoutParams)

答案 1 :(得分:0)

尝试使用此功能。这对我有用。

ArrayList<Component> components;

        LinearLayout llRowContainer;

        llRowContainer = (LinearLayout) findViewById(R.id.ll_row_container);


        components = IappApp.getContext().getDBManager()
                .getComponentList(COMPONENT_TYPE, ACTIVITY_TYPE);


        int index = 0;
        for (Component component : components) {

            View rowView = View.inflate(this, R.layout.dae_general_info_row , null);

            llRowContainer.addView(rowView);

            final TextView txtViewTitle = (TextView) rowView
                    .findViewById(R.id.txtViewTitle);

            final EditText editTextAns = (EditText) rowView
                    .findViewById(R.id.editTextAns);

            editTextAns.setHint(component.getUnit()+"");

            txtViewTitle.setText(++index + ". " + component.getTitle());
            editTextAns.setTag(component);
        }