1

Possible Duplicate:
Getting an issue while checking the dynamically generated checkbox through list view

I have listview with checkboxes.Bydefault all checkboxes are checked.After that some checkboxes unchecked and sroll those checkboxes are checked automatically.But i want after scroll the listview those checkboxes are checked. My code is:

public class  FriendListAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    DrunkMessages friendsList;
    boolean[] checkBoxState;

    public FriendListAdapter(DrunkMessages friendsList) {
        this.friendsList = friendsList;
        if (Utility.model == null) {
            Utility.model = new FriendsGetProfilePics();
        }
        Utility.model.setListener(this);
        mInflater = LayoutInflater.from(friendsList.getBaseContext());

        checkBoxState=new boolean[jsonArray.length()];
    }


    public int getCount() {
        return jsonArray.length();
    }


    public Object getItem(int position) {
        return null;
    }


    public long getItemId(int position) {
        return 0;
    }

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

            View hView = getLayoutInflater().inflate(R.layout.friendslist, null);

            ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic);
            TextView name = (TextView) hView.findViewById(R.id.name);

            final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox);

        profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position]));
        name.setText(names[position]);

        checkbox.setOnClickListener(new View.OnClickListener() {

               public void onClick(View v) {
                if(((CheckBox)v).isChecked()){

                checkBoxState[position]=true;
                status[position]="1";

             Log.e("checked","checked");
            Log.e("Checked",status[position]);
                }
                else{
                 checkBoxState[position]=false;
                status[position]="0";

                Log.e("checked","unchecked");
             Log.e("Checked",status[position]);

                }
               }
               });

      return hView;
}
Community
  • 1
  • 1
rams
  • 1,558
  • 7
  • 25
  • 48
  • instead of inflating another xml for checkbox you can take a listview with simple_list_item_multiple_choice attribute. like this adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice,objectOfList); – sachit Oct 26 '12 at 05:10
  • possible duplicate of [Link1](http://stackoverflow.com/questions/5538867/customlistview-with-checkbox-while-scrolling-check-state-is-interchanged),[Link2](http://stackoverflow.com/questions/6100518/checkbox-auto-call-oncheckedchange-when-listview-scroll) – AppMobiGurmeet Oct 26 '12 at 05:14
  • 1
    http://www.lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html – Lalit Poptani Oct 26 '12 at 05:21

3 Answers3

3

convertview may give you previously generated view so dont rely on it. just store your checkbox state's in one boolean array & change that accordingly depends on checkchangelistener callback here is code for you I just modified getView method of your code everything else keep as is

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

                View hView = getLayoutInflater().inflate(R.layout.friendslist, null);

                ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic);
                TextView name = (TextView) hView.findViewById(R.id.name);

                final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox);

                checkbox.setTag(new Integer(position));  
            profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position]));
            name.setText(names[position]);

            checkbox.setOnCheckedChangeListener(null);
            if(checkBoxState[position])
              checkbox.setChecked(true);
            else  
              checkbox.setChecked(false);

            checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Integer pos = (Integer)buttonView.getTag();     
                    if(isChecked){

                    checkBoxState[pos.intValue()]=true;
                    }
                    else{
                     checkBoxState[pos.intValue()]=false;
                    Log.e("checked","unchecked");

                    }
                   }
                   });

          return hView;
    }
Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
0

Add the following code in your getview method

if(checkBoxState.length>0)
checkbox.setChecked(checkBoxState[position]);
koti
  • 3,681
  • 5
  • 34
  • 58
0

instead of inflating another xml for checkbox you can take a listview with simple_list_item_multiple_choice attribute. like this

 adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_multiple_choice,obje‌​ctOfList);
sachit
  • 1,128
  • 2
  • 12
  • 25