shivani sharma shivani sharma - 4 months ago 14
Android Question

When I scroll down or up the check position will be change every time

When I am checked any item like (position 0) its automatically checked randomly any item (like position 7) and when I scroll down or up the check position will be change every time... I am fed up from that problem...??

public class contactAdpter extends ArrayAdapter<ContactItem> {

Context context;
List<ContactItem> list;

public contactAdpter(Context context, int resource, List<ContactItem> items) {

super(context, resource,items);

this.context = context;
this.list = items;

}

static class ViewHolder {

ImageView imageview;

TextView tv1;

TextView tv2;

CheckBox ch;

}


@Override
public int getCount() {
return list.size();

}


@Override
public int getPosition(ContactItem item) {
return super.getPosition(item);

}

@Override
public long getItemId(int id) {
return id;

}

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


ViewHolder holder=null;

View view = convertView;


ContactItem contactItem = getItem(position);


LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);


if (convertView == null) {

view = mInflater.inflate(R.layout.contact_list, null);

holder = new ViewHolder();

holder.tv1 = (TextView) view.findViewById(R.id.name);
holder.tv2 = (TextView) view.findViewById(R.id.number);
holder.ch = (CheckBox) view.findViewById(R.id.checkbox1);

view.setTag(holder);

} else {

holder = (ViewHolder) view.getTag();

}

holder.tv1.setText(contactItem.getConatct_name());
holder.tv2.setText(contactItem.getNumber());

return view;

}
}

Answer

in ContactItem you need to maintain Checked Status. and change getView as follows,

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

      ViewHolder holder=null;
      View view = convertView; 
      ContactItem contactItem = getItem(position);
      LayoutInflater mInflater = (LayoutInflater) context
             .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
           view = mInflater.inflate(R.layout.contact_list, null);
            holder = new ViewHolder();
            holder.tv1 = (TextView) view.findViewById(R.id.name);
            holder.tv2 = (TextView) view.findViewById(R.id.number);
            holder.ch = (CheckBox) view.findViewById(R.id.checkbox1);
            view.setTag(holder);
        } else {
          holder = (ViewHolder) view.getTag();
        }

       holder.tv1.setText(contactItem.getConatct_name());
       holder.tv2.setText(contactItem.getNumber());

       holder.ch.setOnCheckedChangeListener(null);
       holder.ch.setChecked(contactItem.getCheckedStatus());
       holder.ch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    contactItem.setCheckedStatus(isChecked);
                }
            });
       return view;

     }