lundi 30 mai 2016

Checkbox unchecked when scroll in listview android

I using custom listview in my project and I have one problem. I added checkboxs in row inside listview using addview, but checkbox uncheked when scroll in listview. I tried settag/gettag by position but in my case one row has several checkboxs. so how can I solve this problem? please help me..

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

    View itemView;
    final ViewHolder viewHolder;

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

         viewHolder = new ViewHolder();

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

        time = (TextView) itemView.findViewById(R.id. delivering_item_time);
        name = (TextView) itemView.findViewById(R.id.delivering_item_name);
        address = (TextView) itemView.findViewById(R.id.delivering_item_address);
        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);
        partner_linear.removeAllViews();

        String purchase_name = "";


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


        time.setText(deliveryltemPosition.shipping_time);

        address.setText(deliveryltemPosition.recipient_address);


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

                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);
                viewHolder.checkbox = (CheckBox) addView.findViewById(R.id.partner_goods_chbox);

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

                        viewHolder.checkbox.setId(position);

                    }
                });

                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{
                    }

                }else{

                }


        }

        return itemView;


    }else{
        itemView = convertView;

       viewHolder = new ViewHolder();

        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);
            name = (TextView) itemView.findViewById(R.id.delivering_item_name);
            address = (TextView) itemView.findViewById(R.id.delivering_item_address);
            partner_linear = (LinearLayout) itemView.findViewById(R.id.delivering_partner_goods_linear);
            LayoutInflater layoutInflater =
                    (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            partner_linear.removeAllViews();


            time.setText(deliveryltemPosition.shipping_time);

            address.setText(deliveryltemPosition.recipient_address);

            String purchase_name = "";

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




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


                    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);
                    viewHolder.checkbox = (CheckBox) addView.findViewById(R.id.partner_goods_chbox);


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

                           viewHolder.checkbox.setId(position);

                        }
                    });

                    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{

                        }
                    }else{

                    }
               }
        }
        return convertView;
    }

}




Aucun commentaire:

Enregistrer un commentaire