I'm developing one android application. In this I have listview with checkbox for each item.
By default all the items are checked. For check boxes I have applied onClickListener. Whenever user clicks on the check box, I'm setting the value true in the current positioned bean class.
Everything is fine. But the Ui is updating for two alternate items instead of one. For Example, if I click on the 2nd check box, then the 4th check box is also updating in user interface. On click listener is calling only once.
Below is the code for on click listener in the adapter class.
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mcontext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View listview = convertView;
listview = inflater.inflate(R.layout.edit_group_users_item, null);
//Get all the fields of the layout
userTV = (TextView) listview.findViewById(R.id.code);
checkbox = (CheckBox) listview.findViewById(R.id.checkBox1);
userTV.setText(userList.get(position).getUserName());
checkbox.setChecked(true);
checkbox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Position is:", ""+position);
Log.v("Befoer checkbox status is:", ""+GroupsAdapter.group_users.get(position).isSelected());
if(!GroupsAdapter.group_users.get(position).isSelected()) {
//set the value to true in the user bean
GroupsAdapter.group_users.get(position).setSelected(true);
checkbox.setChecked(true);
} else {
//set the value to false in the user bean
GroupsAdapter.group_users.get(position).setSelected(false);
checkbox.setChecked(false);
}
}
});
return listview;
}
I don't know what was the wrong. Could you please tell me how to solve the above issue.