0

Hi I am Android Developer .Herewith i have mention my code.I have added check box in custom list view i am using Base Adapter .While selected check-box that item only selected very nice but while scrolling after selected check-box went to unchecked automatically .How to make solved this issue.Please give me solution for me .

class LabOrderListAdapter extends BaseAdapter  {
    LayoutInflater mInflater;
    ViewHolder holder;
    Boolean checkboxstate[];
    Context context;

    // DecimalFormat df = new DecimalFormat("#.##");
    public LabOrderListAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        checkboxstate=new Boolean[observationDetailsList.size()];
        for(int i=0;i<checkboxstate.length;i++)
        checkboxstate[i]=false;
        this.context = context;
    }

    public int getCount() {

        if (observationDetailsList != null) {

            Log.i(this.toString(), "observationDetailsList"+observationDetailsList.size());

            return observationDetailsList.size();
        }
        return 0;
    }
    public Object getItem(int arg0) {
        return arg0;
    }
    public long getItemId(int arg0) {
        return arg0;
    }
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.lab_order_listitem, null);
        }
        if (v != null) {
            holder = new ViewHolder();
            holder.txt_Order_code = (TextView) v
            .findViewById(R.id.orderingcode);
            holder.txt_ProcedureCode = (TextView) v
                    .findViewById(R.id.procedurecode);
            holder.txt_Laboratory = (TextView) v
                    .findViewById(R.id.Laboratory);
            holder.billtypeButton = (AprimaButton) v.findViewById(R.id.billtype);
            holder.txt_Order_Description = (TextView) v
                    .findViewById(R.id.description);
            holder.txt_Date = (TextView) v
                    .findViewById(R.id.collectionDate);
            holder.orderCheckBox = (CheckBox) v
                    .findViewById(R.id.checkboxorder);

            holder.txt_TrackingNumber = (TextView) v
                    .findViewById(R.id.accession);


            if(checkboxstate[position]==null){
                checkboxstate[position]=false;
            }
            if(observationDetailsList.get(position).GenerateLabOrder.equalsIgnoreCase("Flase"))
            {
                holder.orderCheckBox.setChecked(checkboxstate[position]);
            }

            if (observationDetailsList.get(position).GenerateLabOrder
                    .equalsIgnoreCase("False")) 
            {
                holder.orderCheckBox.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        if(((CheckBox)v).isChecked()){
                            if (checkboxstate[position] = true)
                            {

                                      patientOrderList.add(orderDetailsList.get(position));
                                      patientObservationList.add(observationDetailsList.get(position));



                            }

                            else{
                                if (checkboxstate[position] = false)
                                {
                                        checkboxstate[position]=false;
                                        patientOrderList.remove(orderDetailsList.get(position));
                                        patientObservationList.remove(observationDetailsList.get(position));
                                }

                            }


                        }
                    }
                });     

            }    
            v.setTag(holder);
        }
        return v;
    }
    }
vimala
  • 1
  • 3
  • http://stackoverflow.com/questions/17168814/how-to-change-the-text-of-a-checkbox-in-listview/17169411#17169411. check this example source https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M – Raghunandan Jul 08 '13 at 12:43
  • for that you have to make a boolean type array in your adapter having length same as your views, then put the value true on the position which checkbox is checked and place a check in get view mwthod which will varify checked from that array which you have created – farrukh Jul 08 '13 at 12:48
  • or check this question http://stackoverflow.com/questions/17489559/android-custom-listview-with-checkbox-how-to-fill-it/17489792#17489792 – farrukh Jul 08 '13 at 12:51
  • i had the same problem. Go through the following link it will solve your problem http://stackoverflow.com/questions/16248940/check-box-in-listview-not-working-properly – Avinash Kumar Pankaj Jul 08 '13 at 12:56

1 Answers1

0

You need to define a Model for storing the checked information of ListItem. on getView() method you need to get the value from Model class for each item.

The smiler example are given in this Blog

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33