以编程方式使用Linearlayout添加文本视图

时间:2016-02-03 10:19:31

标签: android android-linearlayout

我想使用listL以编程方式使用LinearLayout添加2个文本视图。 此代码是一部分listview适配器。

它有错误 - >指定的子项已有父项。您必须先在孩子的父母身上调用removeView()。

如何解决?

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

    View itemView;


    if (convertView == null) {
        itemView = layoutInflater.inflate(R.layout.activity_delivering_partner_item, parent, false);

        final Deliveryltem deliveryltemPosition = epicerieDelivery_delivering_recipient.selectedDeliveryItem.get(position);

        time = (TextView) itemView.findViewById(R.id. delivering_item_time);
        address = (TextView) itemView.findViewById(R.id.delivering_item_address);
        name = (TextView) itemView.findViewById(R.id.delivering_item_name);
        goods = (TextView) itemView.findViewById(R.id.delivering_item_goods);
        partner_linear = (LinearLayout) itemView.findViewById(R.id.delivering_partner_goods_linear);

        LayoutInflater layoutInflater =
                (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View addView = layoutInflater.inflate(R.layout.activity_delivering_partner_item_row, null);

        TextView goods_name = (TextView) addView.findViewById(R.id.partner_goods_name_row);
        TextView goods_ea = (TextView) addView.findViewById(R.id.partner_goods_ea_row);
        String purchase_name = deliveringListActivity.purchase_name_arr.get(deliveringListActivity.purchase_num);


        for(int k = 0; k< deliveringListActivity.partner_goods_arr.size(); k++){


            if(deliveryltemPosition.delivery_order_id.equals(deliveringListActivity.partner_goods_arr.get(k).goods_order_id)){


                if(deliveringListActivity.partner_goods_arr.get(k).detail_purchase.equals(purchase_name)){

                    goods_name.setText(deliveringListActivity.partner_goods_arr.get(k).detail_product_name);
                    goods_ea.setText(deliveringListActivity.partner_goods_arr.get(k).detail_ea);

                    partner_linear.addView(addView);

                }
            }else{

            }
        }
        time.setText(deliveryltemPosition.shipping_time);
        address.setText(deliveryltemPosition.recipient_address);
        name.setText(deliveryltemPosition.recipient_name);


      return itemView;


}else{
        itemView = convertView;

        if(epicerieDelivery_delivering_recipient.selectedDeliveryItem.size() != 0){

            final Deliveryltem deliveryltemPosition = epicerieDelivery_delivering_recipient.selectedDeliveryItem.get(position);

            time = (TextView) itemView.findViewById(R.id. delivering_item_time);
            address = (TextView) itemView.findViewById(R.id.delivering_item_address);
            name = (TextView) itemView.findViewById(R.id.delivering_item_name);
            partner_linear = (LinearLayout) itemView.findViewById(R.id.delivering_partner_goods_linear);
            LayoutInflater layoutInflater =
                    (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(R.layout.activity_delivering_partner_item_row, null);

            TextView goods_name = (TextView) addView.findViewById(R.id.partner_goods_name_row);
            TextView goods_ea = (TextView) addView.findViewById(R.id.partner_goods_ea_row);
            time.setText(deliveryltemPosition.shipping_time);
            address.setText(deliveryltemPosition.recipient_address);
            name.setText(deliveryltemPosition.recipient_name);

            String purchase_name = deliveringListActivity.purchase_name_arr.get(deliveringListActivity.purchase_num);




            for(int k = 0; k< deliveringListActivity.partner_goods_arr.size(); k++){
       if(deliveryltemPosition.delivery_order_id.equals(deliveringListActivity.partner_goods_arr.get(k).goods_order_id)){

                    if(deliveringListActivity.partner_goods_arr.get(k).detail_purchase.equals(purchase_name)){
                        goods_name.setText(deliveringListActivity.partner_goods_arr.get(k).detail_product_name);
                        goods_ea.setText(deliveringListActivity.partner_goods_arr.get(k).detail_ea);

                        partner_linear.addView(addView);
                }
                }else{

                }
            }

        }

      return convertView;
    }
}

3 个答案:

答案 0 :(得分:1)

  

指定的孩子已经有父母。你必须调用removeView()   先关于孩子的父母

由于:

partner_linear.addView(addView);

线。

for-loop内多次添加View的同一对象partner_linear

要使其工作,请在for循环中夸大activity_delivering_partner_item_row布局:

for(int k = 0; k< deliveringListActivity.partner_goods_arr.size(); k++){
  // inflate layout and access all Views from it here
   final View addView = layoutInflater.inflate(
                      R.layout.activity_delivering_partner_item_row, null);
   TextView goods_name = (TextView) 
                      addView.findViewById(R.id.partner_goods_name_row);

 .... your code here...

    partner_linear.addView(addView);
}

答案 1 :(得分:0)

要知道在Android中添加子视图的第一件重要事情是&#34;孩子只能拥有1个父母&#34;这里你要添加的textView已经是xml文件中布局的子项,因此你不能为该子项设置另一个父项。而不是使用FindViewById()尝试通过为TextView类创建对象并为这些textViews设置参数来动态添加TextViews使用LayoutParams。

答案 2 :(得分:0)

您一次又一次地向parent_linear添加相同的视图。 每次在forloop中进行迭代时都需要创建一个新视图

 for(int k = 0; k< deliveringListActivity.partner_goods_arr.size(); k++){
  // inflate layout and access all Views from it here
   final View addView = layoutInflater.inflate(
                      R.layout.activity_delivering_partner_item_row, null);
   TextView goods_name = (TextView) 
                      addView.findViewById(R.id.partner_goods_name_row);

 .... your code here...

    partner_linear.addView(addView);
}