Only the first checkbox is set from a listview by clicking anywhere in the item

3

Good morning. I'm new to the forum and a newbie to android. I created a ListView through a Base Adapter, with checkbox selectable, so far so good. But when I click anywhere on an item, only the checkbox of the first item is set. As in the image, I clicked somewhere on the second item and the checkbox of the first one was set. By simply clicking the checkboxes they are set normally.

I tried the forum but I did not find a solution. If you're sorry for the duplicate. Please, how can I solve this problem? My getView from my adapter.

    public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_list_selected, null);
    }

    ImageView icone = (ImageView)convertView.findViewById(R.id.imageView1);
    TextView nome = (TextView)convertView.findViewById(R.id.myTextViewItemListNomeSelected);
    TextView fone = (TextView)convertView.findViewById(R.id.myTextViewItemListFoneSelected);
    CheckBox cbx = (CheckBox)convertView.findViewById(R.id.myCheckBoxItemListSelected);

    Contato contato = contactList.get(position);

    icone.setImageResource(contato.getImage());
    nome.setText(contato.getNome());
    fone.setText(contato.getTel());
    cbx.setChecked(contato.isCheck());

    cbx.setTag(contato);

    cbx.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            CheckBox check = (CheckBox)v;
            Contato ct = (Contato)v.getTag();
            ct.setCheck(((CheckBox)v).isChecked());

            if(check.isChecked()){
                if(! idSelecteds.contains(ct.getID())){
                    idSelecteds.add(ct.getID());
                }
            }else {
                if(idSelecteds.contains(ct.getID())){
                    idSelecteds.remove(ct.getID());
                }
            }
        }
    });
    return convertView;
}

The implementation of my OnItemClickListener performed this way.

list.setOnItemClickListener(this);

public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub

    CheckBox cb = (CheckBox)findViewById(R.id.myCheckBoxItemListSelected);
    cb.setChecked(true);
}
    
asked by anonymous 24.07.2014 / 15:17

1 answer

1

The problem is that within OnItemClickListener , it is calling the findViewById method of Activity .

The findViewById transverse all Views from the root, returning to the first found . So the first one is the Checkbox of the first line.

You need to execute findViewById on View that is passed by parameter in onItemClick method, is View representing ListView line returned by Adapter .     

24.07.2014 / 16:03