0

When I try to check the CheckBox of an item in a custom listView (where each row contains an image view a text and a checkbox) some others unselected checkbox become checked.

My Adapter is

public class MyAdapter extends BaseAdapter{
    private Activity activity;
    private String[] dataCar;
    private String[] dataImm;

    private static LayoutInflater inflater=null;
    public TitleLoader titleLoader;
    public ImageLoader imageLoader; 
    public TextLoader textLoader;

    public MyAdapter(Activity a, String[] dCar, String[] dImm) {
        activity = a;
        dataCar=dCar;
        dataImm=dImm;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        titleLoader=new TitleLoader(activity.getApplicationContext());
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.elemento_lista_car, null);

        TextView car=(TextView)vi.findViewById(R.id.car);
        ImageView image=(ImageView)vi.findViewById(R.id.image);
        CheckBox cb=(CheckBox)vi.findViewById(R.id.checkBox);
        titleLoader.DisplayTitle(dataCar[position], car);
        imageLoader.DisplayImage(dataImm[position], image);

        return vi;
    }

}
AndreaF
  • 11,975
  • 27
  • 102
  • 168
  • 1
    You've probably stumble upon `ListView'`s recycling mechanism. There are a lot of question on stackoverflow on how to deal with this. Here is one of my previous answers regarding this. http://stackoverflow.com/questions/10057328/cant-set-oncheckedchangelistener-to-a-checkbox/10057730#10057730 – user Sep 29 '12 at 16:56
  • Thanks for the answer. Could you help me to adapt your solution to my adapter? – AndreaF Sep 29 '12 at 17:38
  • 1
    To make it work you just have to follow my code and add what you don't have from my answer: the `ArrayList` of `booleans` and the `OnCheckedChangeListenr` in the `getView` method. – user Sep 29 '12 at 18:20
  • Thank you. Thank you. Thank you. I have fixed all my problems. – AndreaF Sep 30 '12 at 00:32

0 Answers0