1

I am implementing the checkbox with listview for every Iten of listview.The Problem i am getting is when I am clicking on any single check box then some other checkboxes which are not clicked also getting clicked randomly.

public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            View vi = convertView;
            final int as=position;
            if (convertView == null) { // if it's not recycled, initialize some
                // attributes
                LayoutInflater  inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                vi = inflater.inflate(R.layout.grid_item, null);
                holder = new ViewHolder();

                holder.textheader = (TextView) vi.findViewById(R.id.item1);
                holder.textcpu = (TextView) vi.findViewById(R.id.item2);
                holder.text_modified_date = (TextView) vi.findViewById(R.id.item4);
                holder.text_cpu = (TextView) vi.findViewById(R.id.item3);
                holder.text_checkbox=(CheckBox)vi.findViewById(R.id.checkBox1);
                vi.setTag(holder);

                holder.text_checkbox.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (holder.text_checkbox.isClickable()) {
                            itemChecked.set(as, true);
                            packages=as;
                        } else if (!holder.text_checkbox.isClickable()) {
                            itemChecked.set(as, false);
                        }
                        String packag=app_package.get(as);
                        Log.d("package listttttttttt", packag);
                    }
                });


            } else {
                holder = (ViewHolder) vi.getTag();


            }
user1522869
  • 109
  • 2
  • 13

3 Answers3

3

Try below code

public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View vi = convertView;
        final int as=position;
        if (convertView == null) { // if it's not recycled, initialize some
            // attributes
            LayoutInflater  inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            vi = inflater.inflate(R.layout.grid_item, null);
            holder = new ViewHolder();

            holder.textheader = (TextView) vi.findViewById(R.id.item1);
            holder.textcpu = (TextView) vi.findViewById(R.id.item2);
            holder.text_modified_date = (TextView) vi.findViewById(R.id.item4);
            holder.text_cpu = (TextView) vi.findViewById(R.id.item3);
            holder.text_checkbox=(CheckBox)vi.findViewById(R.id.checkBox1);
            vi.setTag(holder);

        } else {
            holder = (ViewHolder) vi.getTag();
        }          


             holder.text_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() 
             {          
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)      
                {                                  
                if(isChecked)
                {
                      // do your stuff
                      itemChecked.set(as, true);
                        packages=as;
                }
                else
                {
                      // do your stuff
                      itemChecked.set(as, false);
                }       

        }
    });

if(itemChecked.size() != 0 && itemChecked.get(position))
        holder.text_checkbox.setChecked(true);
    else
        holder.text_checkbox.setChecked(false);
Braj
  • 2,164
  • 2
  • 26
  • 44
  • I made small change check now... by d way what itemChecked.set(as, true); does ? – Braj Aug 23 '12 at 06:42
  • Not able to set setOnCheckedChangeListener method. itenChecked.set the is to set the flag true if item is checked and as is position – user1522869 Aug 23 '12 at 06:51
  • setOnCheckedChangeListener is a standard method callback for checkBox. You should b able to use it. Whats wrong with it, I mean, is it giving any error? – Braj Aug 23 '12 at 06:58
  • Now I am able to set the method setOnCheckedChangeListener,but still facing same problem – user1522869 Aug 23 '12 at 07:22
  • I hv edited my answer... check now. I used itemChecked. If its noe correct, make changes yourself – Braj Aug 23 '12 at 07:30
1

The problem is that your getView() method doesn't deal properly with views. You have to setup the state of new views and update the state of recycled views. In addition you have to define the behavior of the views. There are a lot of threads on SO with similar problems. I think this one can help you (it contains working code).

Your code should look like the following skeleton:

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    final int as=position;
    if (convertView == null) { // no view passed, create one
        LayoutInflater  inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi = inflater.inflate(R.layout.grid_item, null);
        // Setup the View content
        holder = new ViewHolder();
        holder.textheader = (TextView) vi.findViewById(R.id.item1);
        holder.textcpu = (TextView) vi.findViewById(R.id.item2);
        holder.text_modified_date = (TextView) vi.findViewById(R.id.item4);
        holder.text_cpu = (TextView) vi.findViewById(R.id.item3);
        holder.text_checkbox=(CheckBox)vi.findViewById(R.id.checkBox1);
        // Do your checkbox initial setup here. 
        holder.text_checkbox.setChecked(getBooleanFromPosition(position));
        vi.setTag(holder);
        // Setup the View behavior
         holder.text_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() 
         {          
              // Do your stuff here
    });
    } else {
        // Update your View state
        holder = (ViewHolder) vi.getTag();
        holder.text_checkbox.setChecked(getBooleanFromPosition(position));
    }                   

Obviously you have to store the state of your checkboxes in some data structure, for instance a boolean array and read that data structure in order to know if the checkbox of a given position is checked/unchecked (that read access is what the getBooleanFromPosition is supposed to do). And that data structure should be updated in the section that controls the View behavior.

Community
  • 1
  • 1
Vicent
  • 5,322
  • 2
  • 28
  • 36
1

The problem is that you are not allowed to have a focus-able item work in conjunction with a list view, but there is a way around this:

If you are using an XML layout as your list item, set the checkbox focusable attribute to false:

android:focusable="false"

This should cause the list item to be clickable as well.

I hope this solves your problem. Please let me know how it goes.

Ushal Naidoo
  • 2,704
  • 1
  • 24
  • 37