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

Asked

Viewed 198 times

3

good morning. I’m new to the forum and beginner to android. I created a Listview through a Base Adapter, with selectable checkbox, so far so good. But when I click anywhere on any item, only the first item’s checkbox is set. As in the image, I clicked somewhere on the second item and the checkbox of the first one was set. Just by clicking on the checkbox they are set normally.

inserir a descrição da imagem aqui

I already searched the forum but could not find a solution. If you are 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 carried out in 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);
}

1 answer

1


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

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

You need to run the findViewById in View which is passed by parameter in the method onItemClick, is the View which represents the line of ListView returned by Adapter.

  • Wakim, thank you very much. Problem solved. </br>public void onItemClick(Adapterview<?> Parent, View view, int position, long id) { // TODO Auto-generated method stub Checkbox check = (Checkbox)view.findViewById(R.id.myCheckBoxItemListSelected); check.setChecked(true); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.