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.
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);
}
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); }
– Marcelo Viana