I've searched around for a solution to my problem but couldn't find one so here it is.
I have a arraylist of data that i display using a combination of textview and checkbox. This is done using custom adapter which basically inflates it row by row. Everything up to this point is fine until i want to retrieve the checked data.
What i read from other people is that their checked boxes becomes unchecked when scrolled due to the recycling of the view. For me the state of the checkboxes remain but i don't know how to retireve them without scrolling it. When i check a box it is "check" on the UI but actaully it is not recognized until i scroll the checked item out of view and back in again.
Currently i use a global arraylist to populate with list.get(position).getName() if the list.get(position).isSelected() == true. But you see this arraylist remains empty unless i scroll the item out of view and back again. the same applies when the boxes are unchecked, they remain in the arraylist until i scroll. What i have in mind is a submit button that takes all the checkbox's current state when it is clicked then construct the arraylist.
any idea how to create such thing?
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.qlist, 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();
//
if(isChecked){
selctionCount++;
}
else if (!isChecked){
selctionCount--;
}
if(selctionCount > 2)
{
Toast.makeText(context, "error, you checked more than 2!", Toast.LENGTH_LONG).show();
element.setSelected(false);
}
else
{
element.setSelected(buttonView.isChecked());
}
System.out.println(selctionCount);
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
}
else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
//This is the problem here! the selected arraylist (global) is empty even when i check the boxes,
//it only populates when i scroll the checked boxes out of view and back in! helphelp!
holder.checkbox.setChecked(list.get(position).isSelected());
if (list.get(position).isSelected() == true){
selected.add(list.get(position).getName());
}
else if (list.get(position).isSelected() == false){
selected.remove(list.get(position).getName());
}
return view;
}
}