I'm looking to implement a list view with multiple check boxes. The user should be able to select multiple check boxes and click on 'submit' button to proceed further.
I'm using a custom adapter to display my own list view and override getView()
method.
So far so good, I'm able to display the list with check boxes and I can select multiple check boxes, but when I'm trying to get the selected list item details (using the list.get(position).getName()
), it is started bouncing. I mean, when I start scrolling the list view to select further items, the value of the above is changing to different items and not keeping the original.
My code is here:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.checkboxlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
//Toast.makeText(getContext(), "You just checked: "+list.get(position).getName(), Toast.LENGTH_SHORT).show();
System.out.println("The element name is: "+element.getName());
System.out.println("Name of the checked item is--outside loop:"+list.get(position).getName());
}
});
Is there a way to constrain or hold the value of the items I selected even after keep scrolling to check for other items.