I have a ListView
where each item contains a TextView
and a Checkbox
, I save the state of each line in internal files, and when a checkbox is checked, I change said state for the correct line.
Here is my Adapter
:
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.todo_list_layout, parent, false);
// Get line concerned and parse it
String currentTodo = values.get(position);
String[] todoArray = currentTodo.split(" \\| ");
String title = todoArray[0];
String checked = todoArray[1];
TextView titleTextView = (TextView) rowView.findViewById(R.id.title);
titleTextView.setText(title);
handleCheckBox(rowView, checked, position);
return rowView;
}
private void handleCheckBox(View rowView, String checked, final int position) {
final CheckBox checkBox = (CheckBox) rowView.findViewById(R.id.checkbox);
checkBox.setOnClickListener(new View.OnClickListener() {
/**
* User clicks on checkbox, change state in file
*/
@Override
public void onClick(View arg0) {
final boolean isChecked = checkBox.isChecked();
IFM.changeCheckBoxState(position, isChecked);
}
});
// If the file says it is true, check it
if (checked.equals("true\n")) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
}
But for some reason this doesn't always have the intended behavior. It does change the state in file when checked (and on the right line), but when scrolling the view out and then back in, sometimes the state is recycled and is lost.
I have read a few threads such as Checkbox unchecked automatically while scrolling listview, but did not find any answers that could help me.
Aucun commentaire:
Enregistrer un commentaire