I have a list of checkboxes (created via custom list adapter), some of which start checked based on stored data. When the user presses another button, I'd like to check which boxes are checked. Right now it starts with two boxes, one of which is clicked. When I click the checkboxes the UI changes appropriately, but when I do the "gather checked" function, it reports the original configuration, not the changed one.
The XML for the row:
CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default"
android:id="@+id/cause_checkbox"
xmlns:android="http://ift.tt/nIICcg"
The List adapter: private class CauseEntryAdapter extends ArrayAdapter { List selectedCauseKeys; List availableCauseKeys;
public CauseEntryAdapter(Context context, List<String> causeLabels, List<String> availableCauseKeys, List<String> selectedCauseKeys) {
super(context, 0, causeLabels);
if (selectedCauseKeys != null)
this.selectedCauseKeys = selectedCauseKeys;
else
this.selectedCauseKeys = new LinkedList<>();
this.availableCauseKeys = availableCauseKeys;
}
public View getView(int position, View convertView, ViewGroup parent) {
String cause = (String) getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.cause_list_item, parent, false);
}
CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.cause_checkbox);
checkbox.setText(cause);
checkbox.setChecked(selectedCauseKeys.contains(availableCauseKeys.get(position)));
return convertView;
}
}
The code to collect checked boxes:
public List<String> getCheckedCauses() {
List<String> ret = new LinkedList<String>();
for(int i = 0; i< mAdapter.getCount(); i++) {
CheckBox checkBox = (CheckBox) mAdapter.getView(i, null, null).findViewById(R.id.cause_checkbox);
if (checkBox.isChecked()) {
ret.add((String) mAdapter.getItem(i));
}
//TODO why is this picking up preferences not the list?
}
return ret;
}
The list lives in a fragment, I invoke it with:
CauseListFragment causeListFragment = (CauseListFragment) fragmentManager.getFragments().get(0);
return causeListFragment.getCheckedCauses();
Aucun commentaire:
Enregistrer un commentaire