Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi i'm new to android and doing custom listview with checkbox, in that i'm facing random selection of another list item checkbox while scrolling. I have gone through some threads but didn't solve my problem. Pls help me out from this. Here is my custom adapter

CustomAdapter.java

public class CustomAdapter extends BaseAdapter{

private boolean[] checkBoxState;
private final Activity context;
public final ArrayList<SelectedListModel>list;
private SelectedListModel element;
    private ArrayList<SelectedListModel> positions = new ArrayList<SelectedListModel>();

public CustomAdapter(Activity applicationContext,
        ArrayList<SelectedListModel> contacts) {
    // TODO Auto-generated constructor stub
    this.context = applicationContext;
    this.list = contacts;
    this.checkBoxState=new boolean[contacts.size()];
}

static class ViewHolder {
    protected TextView name,number;
    protected CheckBox checkbox;
  }


public int getCount() {
    // TODO Auto-generated method stub
    System.out.println("list size:"+list.size()); 
    return list.size();
}

public ArrayList<SelectedListModel> getSelectedItemList() {
    // TODO Auto-generated method stub
    return positions;
}

public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder viewHolder;
    if (convertView == null) {
      LayoutInflater inflator = context.getLayoutInflater();
      convertView = inflator.inflate(R.layout.row_listview, null);
      viewHolder = new ViewHolder();
      viewHolder.name = (TextView) convertView.findViewById(R.id.textView1);
      viewHolder.number = (TextView) convertView.findViewById(R.id.textView2);
      viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
      viewHolder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            element = (SelectedListModel)viewHolder.checkbox.getTag();
            if(buttonView.isChecked()==true){
                checkBoxState[position] = buttonView.isChecked();
                element.setSelected(checkBoxState[position]);
                positions.add(element);


            }else{
                checkBoxState[position] = buttonView.isChecked();
                element.setSelected(checkBoxState[position]);

                positions.remove(element);
            }

        }
    });


      viewHolder.checkbox.setTag(list.get(position));
      convertView.setTag(viewHolder);
    } else {

        viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) convertView.getTag();
    setNameAndNumber(list.get(position),holder,position);
    return convertView;
  }

private void setNameAndNumber(SelectedListModel selectedListModel,ViewHolder holder, int position) 
{
    // TODO Auto-generated method stub
    holder.name.setText(selectedListModel.getName());
    holder.number.setText(selectedListModel.getNumber());
    holder.checkbox.setSelected(selectedListModel.isSelected());


}

}

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You're setting the "selected" state but not the "checked" state:

holder.checkbox.setSelected(selectedListModel.isSelected());
holder.checkbox.setChecked(selectedListModel.isSelected());
share|improve this answer
    
Thanks man its fixed the auto selection.. but still getting some problem for example if i have 15 list item & scrolled to 10th item and selected both. Now i am not getting the exact list item instead getting 3rd item as selected one. –  saravana May 13 '13 at 6:17
    
Don't exactly get what the problem is now, but you're keeping track of the selections in three places (boolean array, selected list, the model), I would suggest you simplify it and only track it in the model. That might at least clarify what's going on. –  dmon May 13 '13 at 14:24
    
Thanks again..Have another one question If i add data dynamically to the arraylist from another activity, that replaces the older data in the arraylist..so how to handle. –  saravana May 15 '13 at 18:49
    
"From another activity" <--- If this is modifying the data directly then it sounds like bad design. If you want to "refresh" the data your adapter needs to be able to handle merging the current model with the new one. –  dmon May 15 '13 at 19:02
    
ok. If you have any example could you please send me a link –  saravana May 17 '13 at 19:30
mViewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox1);
            mViewHolder.checkBox.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);
                    if (cb.isChecked()) {
                        checkBoxArrayList.set(position, true);
                    } else if (!cb.isChecked()) {
                        checkBoxArrayList.set(position, false);
                    }
                }
            });

Hi below link may help for you http://mylearnandroid.blogspot.in/2014/06/listview-problem-while-scrolling.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.