0

Hi i am creating a custom list view with dynamic check box adding that i am getting what i want but when i try to select one check box in first row then automatically first button in 5th ,9th,13 row is getting selected and when i select any button in second row then same button in 6th,8th,12th row is getting selected what i am doing wrong here and my adapter class

class listViewEventscustomAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    ViewHolder holder;

    public listViewEventscustomAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        if (arrListViewoption_title != null) {
            return arrListViewoption_title.size();
        } else {
            Toast.makeText(getApplicationContext(), "No item found",
                    Toast.LENGTH_LONG).show();
            return 0;
        }
    }

    public Object getItem(int arg0) {
        return arg0;
    }

    public long getItemId(int arg0) {
        return arg0;
    }

    @SuppressWarnings("unchecked")
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View v = convertView;
        final CheckBox checkitem;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.customlistviewdata, null);
        }
        if (v != null) {
            holder = new ViewHolder();
            holder.txtoptiontitle = (TextView) v
                    .findViewById(R.id.optiontitle);
            holder.txtsubtitle = (TextView) v.findViewById(R.id.subtitle);
            checkitem = (CheckBox) v.findViewById(R.id.checkBox1);
            holder.btn_radio = (RadioButton) v
                    .findViewById(R.id.radio0);

            holder.txtoptiontitle.setText(arrListViewoption_title
                    .get(position));
            holder.txtsubtitle.setText(arrlistViewsub_title.get(position));

            if (value.equals(arrListViewoption_type.get(position)
                    .toString().trim())) {
                checkitem.setVisibility(View.VISIBLE);
                holder.btn_radio.setVisibility(View.GONE);

            }

            else {
                holder.btn_radio.setVisibility(View.VISIBLE);
                checkitem.setVisibility(View.GONE);

            }

            checkitem
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton view,
                                boolean isChecked) {
                            // int getPosition = (Integer) view.getTag();
                            // list.get(getPosition).setSelected(view.isChecked());

                            if (view.isChecked()) {

                                checkboxvalue.add(arrlistViewsub_title
                                        .get(position).toString().trim());
                            } else {
                                System.out
                                        .println("*************unchecked");
                                checkboxvalue.remove(arrlistViewsub_title
                                        .get(position).toString().trim());
                                adapter_list.notifyDataSetChanged();

                            }

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

                        }
                    });

            holder.btn_radio
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(
                                CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub
                            if (buttonView.isChecked()) {


                                // Action Doing here

                            }
                        }
                    });

            v.setTag(holder);

        }
        return v;

    }

}
Gomathi
  • 41
  • 1
  • 6

1 Answers1

1

Using the convertview is usefull for performancce and fast scrolling but can lead to that kind of problem.

you should reset any set values from the convert view before reusing it. In your case :

boolean wasItemChecked = checkboxvalue.contains(arrlistViewsub_title.get(position).toString().trim());
checkItem.setChecked(wasItemChecked );

if the checkItem state has changed, it reset it to its true state according to the datasource state.

hope that helps.

Guian
  • 4,563
  • 4
  • 34
  • 54